[rspec-users] How to test update_attribute with mocha?

Phlip
Thu, 14 May 2009 04:01:41 -0000

anmaxp wrote:

> I''m having sort of an issue here, I''m trying to test the
update of the
> last_login field upon login, here is my test
>
>   it "assigns a new last_login timestamp" do
>     User.expects(:update_attribute).at_least_once.returns(true)

User.any_instance.expects(:update_attribute).with(:field, value).etc.

An instance of User will itself call the .update_attribute. The class
won''t call it.

However, I can''t think of a reason not to just write the record, reload
it, and
check the fields. And a _functional_ test should not care how the record got
written (update_attribute, save, or whatever). A unit test might care, but
functional tests need a little more float...