Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionMailer testing with rspec [closed]

I am developing a Rails 4 application which involves sending / receiving emails. For example, I send emails during user registration, user comment, and other events in the app.

I have created all emails using the action mailer, and I used rspec and shoulda for testing. I need to test if the mails are received correctly to the proper users. I don't know how to test the behavior.

Please show me how to test an ActionMailer using shoulda and rspec.

like image 219
Dinkaran Ilango Avatar asked Nov 14 '13 16:11

Dinkaran Ilango


People also ask

How do I test an RSpec file?

To run a single Rspec test file, you can do: rspec spec/models/your_spec. rb to run the tests in the your_spec. rb file.

Is RSpec used for unit testing?

RSpec is a unit test framework for the Ruby programming language. RSpec is different than traditional xUnit frameworks like JUnit because RSpec is a Behavior driven development tool.

What type of testing is RSpec?

RSpec is a testing tool for Ruby, created for behavior-driven development (BDD). It is the most frequently used testing library for Ruby in production applications. Even though it has a very rich and powerful DSL (domain-specific language), at its core it is a simple tool which you can start using rather quickly.


1 Answers

How to test ActionMailer with RSpec

  • works for Rails 3 and 4
  • this information has been taken from a good tutorial

Assuming the following Notifier mailer and User model:

class Notifier < ActionMailer::Base   default from: '[email protected]'    def instructions(user)     @name = user.name     @confirmation_url = confirmation_url(user)     mail to: user.email, subject: 'Instructions'   end end  class User   def send_instructions     Notifier.instructions(self).deliver   end end 

And the following test configuration:

# config/environments/test.rb AppName::Application.configure do   config.action_mailer.delivery_method = :test end 

These specs should get you what you want:

# spec/models/user_spec.rb require 'spec_helper'  describe User do   let(:user) { User.make }    it "sends an email" do     expect { user.send_instructions }.to change { ActionMailer::Base.deliveries.count }.by(1)   end end  # spec/mailers/notifier_spec.rb require 'spec_helper'  describe Notifier do   describe 'instructions' do     let(:user) { mock_model User, name: 'Lucas', email: '[email protected]' }     let(:mail) { Notifier.instructions(user) }      it 'renders the subject' do       expect(mail.subject).to eql('Instructions')     end      it 'renders the receiver email' do       expect(mail.to).to eql([user.email])     end      it 'renders the sender email' do       expect(mail.from).to eql(['[email protected]'])     end      it 'assigns @name' do       expect(mail.body.encoded).to match(user.name)     end      it 'assigns @confirmation_url' do       expect(mail.body.encoded).to match("http://aplication_url/#{user.id}/confirmation")     end   end end 

Props to Lucas Caton for the original blog post on this topic.

like image 54
CDub Avatar answered Oct 19 '22 13:10

CDub