Monday, September 20, 2010

Getting over assert_difference

For quite a while now I have been using assert_difference or its close cousin from shoulda should_change. When I recently updated to a recent version of shoulda I noticed that they had deprecated should_change. The note in the shoulda documentation says the following: "This macro was deprecated because these tests aren’t as valuable as alternative tests that explicitly test the final state."

Ok, now what to do? assert_change is super easy to use, but does it really ensure that the desired action is actually happening? For example:

This example test asserts that the number of User objects in the database has increased by 1. No real guarantee that it was actually the User object we were interested in creating.

Lately I have been trying to be a little more explicit about what is actually happening. It is taking a little while to get used to doing things this way but the results have been satisfying. Here is an example:

This example test asserts that a User object is getting assigned to the @user instance variable and that User object is no longer a new record.

1 comment:

David said...

Thanks for putting your thoughts down on this, I still believe should_change is still useful in many cases, but your solution seems to be a good 80/20 fix.

I've taken it a bit further by defining my own shoulda macro in test/shoulda_macros/functionals.rb:

def self.should_save(obj)
should('save'){ assert !assigns(obj).new_record? }

Now I can swap out a good majority of should_change with should_save(:user)