On one of the projects that I am working on, there was an after_create
controller filter which I wanted to refactor into an observer. The
actual refactoring was pretty painless but testing it proved to be
more work than I thought.
The first thing that I noticed was that observer callbacks were not actually
called when running my tests, which is good since it means that my
regular tests weren’t affected.
The observer is for a comment model which basically notifies the owner
of the commentable object that a new comment was posted.
To add support for creator/updater fields, I am using the
For the purpose of explaining how the test goes, lets assume that the
observer looks like the following:
I created a mailer_macros.rb file and included it in spec_herlp.rb. The file contains the following:
The first test that we’d like to do is to test that no emails will get
sent if the commentable creator is the same user as the comment
The second test will be to use different users to create the
comment and commentable objects and checking to see if the commentable creator will be the
recipient of the notification email.
That’s pretty much the basics to start testing observers and delayed_job