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.

Run your specs in logical groups

I stumbled upon this StackOverflow question: testing – Organizing rspec 2 tests into ‘unit’ and ‘integration’ categories in rails – Stack Overflow.

Basically, the guy asks how he can run specs isolated into logical groups, running the unit without running the integration and so on.

At Gogobot, we have exactly that, since we run our specs in the CI, we can decide which to run, and they run one after the other and dependent on each other, so if you fail UNIT, the build fails, no need to continue on.

You can run the rakes like so

 

This is what our spec.rake looks like

 

A solution for the “I Follow” bug with cucumber and Capybara

While working on a new application and writing tests I encountered a problem:
This is the test I have been using:

[ruby]
Scenario: Deleting a project from the main page
Given I am on the account home page
When I follow "Delete Project"
Then I should see "Your project has been deleted"
[/ruby]

The error that was shown is this:

[shell]
undefined local variable or method `node’ for #<Capybara::Driver::Node tag="a" …
[/shell]

Here’s a screen shot of the test run:
Screen shot 2010-11-23 at 5.36.24 PM

After browsing around for quite some time, I found that the error is originated with this line in env.rb file:

[ruby]
require ‘cucumber/rails/capybara_javascript_emulation’ # Lets you click links with onclick javascript handlers without using @culerity or @javascript
[/ruby]

Commenting out this line solved the problem for me and now I can continue with my testing. I hope it will solve your problem as well.