should_change

11 views
Skip to first unread message

joelmoss

unread,
Sep 21, 2009, 10:16:01 PM9/21/09
to shoulda
So I'm trying to test a simple REST update action like this:

context "on PUT to :update" do
setup do
UserSession.create(Factory(:user))
put :update, :id => Factory(:user).to_param, :user => { :email
=> "cl...@dailyplanet.com" }
end

should_change("the users email", :to => "cl...@dailyplanet.com")
{ assigns[:user].email }
should_redirect_to("user profile") { account_path }
end

But i cannot for the life of me get the assigned :user within the
should_change block. I get this error:

NoMethodError: You have a nil object when you didn't expect it!
The error occurred while evaluating nil.assigns

Please help anyone?

Dan Croak

unread,
Sep 22, 2009, 8:38:10 AM9/22/09
to sho...@googlegroups.com
Try parentheses instead of brackets on assigns.

Dan

On Sep 21, 2009, at 10:16 PM, joelmoss <jo...@developwithstyle.com>
wrote:

Joel Moss

unread,
Sep 22, 2009, 8:49:44 AM9/22/09
to sho...@googlegroups.com
Yeah I already tried that. Banging me head against my desk right now, so any help at all would be appreciated, thx.

Is that the way that you usually test for a record change/update?

Joel Moss

Develop with Style at http://DevelopWithStyle.com
Really useful software project management: http://Codaset.com
Tweet me at http://twitter.com/joelmoss
Message me on AIM: joelkmoss and MSN: jo...@joelmoss.info


2009/9/22 Dan Croak <dcr...@thoughtbot.com>

Dan Croak

unread,
Sep 22, 2009, 9:13:43 AM9/22/09
to sho...@googlegroups.com
Joel,

On Tue, Sep 22, 2009 at 8:49 AM, Joel Moss <jo...@developwithstyle.com> wrote:

> Is that the way that you usually test for a record change/update?

At the controller level, I would only test that the correct
ActiveRecord method was invoked:

should "update attributes and redirect to account on PUT to :update" do
user = Factory(:user_session)
UserSession.stubs(:find).returns(user)
user.stubs(:update_attributes).returns(true)

put :update, :id => user.to_param,
:user => { :email => "cl...@dailyplanet.com" }

assert_received(user, :update_attributes) do |expects|
expects.with({ :email => "cl...@dailyplanet.com" })
end

assert_redirected_to account_path
end

I know that update_attributes will update the record. So, I just need
to test that it is invoked during the action.

The assert_received syntax is a test spy in jferris/mocha:

http://robots.thoughtbot.com/post/159805295/spy-vs-spy

> 2009/9/22 Dan Croak <dcr...@thoughtbot.com>
>>
>> Try parentheses instead of brackets on assigns.
>>
>> Dan
>>
>> On Sep 21, 2009, at 10:16 PM, joelmoss <jo...@developwithstyle.com>
>> wrote:
>>
>> >
>> > So I'm trying to test a simple REST update action like this:
>> >
>> >    context "on PUT to :update" do
>> >      setup do
>> >        UserSession.create(Factory(:user))
>> >        put :update, :id => Factory(:user).to_param, :user => { :email
>> > => "cl...@dailyplanet.com" }
>> >      end
>> >
>> >      should_change("the users email", :to => "cl...@dailyplanet.com")
>> > { assigns[:user].email }
>> >      should_redirect_to("user profile") { account_path }
>> >    end
>> >
>> > But i cannot for the life of me get the assigned :user within the
>> > should_change block. I get this error:
>> >
>> >    NoMethodError: You have a nil object when you didn't expect it!
>> >    The error occurred while evaluating nil.assigns
>> >
>> > Please help anyone?
>> >
>> > >
>>
>>
>
>
> >
>



--
Dan Croak
@Croaky

Joel Moss

unread,
Sep 22, 2009, 9:58:39 AM9/22/09
to sho...@googlegroups.com
Thx, I'll give that a go.

Joel Moss

Develop with Style at http://DevelopWithStyle.com
Really useful software project management: http://Codaset.com
Tweet me at http://twitter.com/joelmoss
Message me on AIM: joelkmoss and MSN: jo...@joelmoss.info


2009/9/22 Dan Croak <dcr...@thoughtbot.com>

david.l...@gmail.com

unread,
Sep 22, 2009, 11:43:42 AM9/22/09
to sho...@googlegroups.com
The issue is that should_change invokes its code block both before and
after the context setup block.
Before the setup block, and thus before the request, no such assigns
variable exists.

-David
Reply all
Reply to author
Forward
0 new messages