Testing the Notifier class with Rspec

Posted on Apr 8, 2012

Today, I needed to spec the Notifier (mailer) class in a Rails project.

I don’t mean to spec the view, I just needed to test if the Notifier class method was being called with some params.

For example, I had this user story

As a User

When I am an owner of an issue

And the issue status changes

I want to get an email

So, all I had to to in my spec, is to test if the Notifier class was being called, I am testing the view in the notifier_spec, so no need to test it twice.

So, I wrote this code.

When I ran the spec, I was surprised to see this error:

After scratching my head for a while, I realized that since I used mocks and expectations for the Notifier class, the underlying code was never called, so it wasn’t returning the Mail::Message class.

I fixed it by doing this:

And now I have a happy spec file, that is only testing the messages between the classes, which is what I wanted to do in the first place.