Testing Email with RSpec
I ran into two types of errors when testing if emails triggered in the code with RSpec.
The first issue was that RSpec was not correctly determining if an email was triggered. I knew the email was getting triggered correctly, but my test was failing. The reason was that I was putting the should matcher too late.
I was putting this after calling the method I was testing (i.e. post :create) like I do with all of my other matchers. However, with Mailer.should_receive, it should go before the call.
The second issue I had was that I was getting the error message:
Undefined method ‘deliver’ for nil:NilClass
For this, I had to stub out the response, like so:
Mailer.should_receive(:email_name).and_return(double(“Mailer”, :deliver => true))