Consider following scenario. Your domain consist of profile model that belongs to user model, and you want to assure that once user is being created appropriate profile is being created for this user, therefore there are no “profile-less” users. You can easily implement such feature using Active Record’s callbacks:
Now you want to write profile specification that checks if profile can be successfully saved given a user. You can implement it like this:
Note usage of FactoryGirl instead of fixtures.
If you run this test it will fail:
save! method is called once, however it looks that we’ve got 2
Profile objects being saved. How could it be?
The problem is caused because
subject in RSpec are lazily
evaluated. This is what happened. First
Profile.count is evaluated
to fetch its current value (before it is changed). Next the expect
block is evaluated. In this block we call
profile which build the
Profile object. While building profile
user is called which
creates the user. However
User model has a callback creating new
profile, here first profile is being saved. Next we call
save! on just
constructed profile saving second profile instance. Now
Profile.count returns 2 instead of expected 1.
To fix the issue we need to force RSpec not to lazily evaluate
creation of user object. We can do this using