Testing the Notifier class with Rspec

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.

Submit to Hacker News

[NOTE] This post has been imported to Hugo from a Wordpress blog. It may be broken, missing images, code snippets or other information.

[NOTE] updated April 8, 2012. It's 3027 days old . This article may have outdated content.