Có good tutorial về cách kiểm tra ActionMailer bằng RSpec. Đây là thực tế tôi đã theo dõi và nó đã không làm tôi thất bại.
Các hướng dẫn sẽ làm việc cho cả hai Rails 3 và 4.
Trong trường hợp các hướng dẫn trong liên kết ở trên phá vỡ, đây là những bộ phận liên quan:
Giả sử Notifier
bưu phẩm và User
mô hình sau:
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
Và cấu hình thử nghiệm sau đây:
# config/environments/test.rb
AppName::Application.configure do
config.action_mailer.delivery_method = :test
end
Những thông số kỹ thuật sẽ giúp bạn có những gì bạn muốn:
# 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 để Lucas Caton cho bài đăng blog ban đầu về chủ đề này.
Tại sao câu hỏi này lại bị đóng làm chủ đề? –