I’m testing one of my mailers, and I have a controller test that just asserts that the mailer gets called (very similar to the example from email_spec).
# spec/controllers/friendships_controller_spec.rb
it "sends an email when someone sends you a contact request" do
NotificationMailer.any_instance.should_receive(:contact_request).with(default_user, user)
xhr :post, :create, {:friendship => {:friend_id => user.id}}
end
This spec fails due to a a variable being set to nil in the template for contact_request. So it seems like… the contact_request method is actually executing and rendering its template, even though I have the method mocked up. How come?
If it makes any difference, the template renders just fine in the actual application – the variable is only set to nil in this particular spec.
Try removing
any_instancefrom the stub chain? Isn’t it called in your code via:NotificationMailer.contact_request?any_instanceis assuming it was called on an instance of the class, not on the class itself.