Suppose you have an ActiveRecord::Observer in one of your Ruby on Rails applications – how do you test this observer with rSpec?
Suppose you have an ActiveRecord::Observer in one of your Ruby on Rails applications –
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You are on the right track, but I have run into a number of frustrating unexpected message errors when using rSpec, observers, and mock objects. When I am spec testing my model, I don’t want to have to handle observer behavior in my message expectations.
In your example, there isn’t a really good way to spec ‘set_status’ on the model without knowledge of what the observer is going to do to it.
Therefore, I like to use the ‘No Peeping Toms’ plugin. Given your code above and using the No Peeping Toms plugin, I would spec the model like this:
You can spec your model code without having to worry that there is an observer out there that is going to come in and clobber your value. You’d spec that separately in the person_observer_spec like this:
If you REALLY REALLY want to test the coupled Model and Observer class, you can do it like this:
99% of the time, I’d rather spec test with the observers turned off. It’s just easier that way.