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.

2 comments:

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_assign_to(obj)
should('save'){ assert !assigns(obj).new_record? }
end

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

oberoneeaster said...

The safety of on-line playing certainly one of the|is among the|is doubtless one of the} most frequent concerns. If you want a secure and fair recreation play the best thing capable of to} do} is to decide on} a reputable site and choose a reliable cost choice. All the video games provided by on-line casinos use Random Number Generator which ensures that they're provably fair. What are the best on-line on line casino video games obtainable to gamers in South Korea? Since so much of} on-line casinos at your disposal, with all of them offering broad range|a variety} of video games from 1xbet many different providers, the answer to which video games are the best depends on by} what kind of video games you like.