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,
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