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

Phlip phlip2005 at gmail.com
Thu May 14 00:01:41 EDT 2009

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


More information about the rspec-users mailing list