Created
January 7, 2014 08:14
-
-
Save bigfive/8296139 to your computer and use it in GitHub Desktop.
ActionMailer RSpec helpers. Makes mailer test more similar to controller tests in that you can test that instance variables are assigned for rendering
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "spec_helper" | |
describe ExampleMailer do | |
describe :new_user do | |
let(:user) { FactoryGirl.create :user } | |
it "sends to the correct addresses" do | |
# :mail is similar to the controller specs 'get', 'post' etc methods | |
mail :new_user, user | |
# :response is similar to controller specs, it returns the mail message | |
expect(response.from).to include '[email protected]' | |
expect(response.to).to include '[email protected]' | |
end | |
it "assigns @user" do | |
mail :new_user, user | |
# :assigns is similar to controller specs, it checks the view_assigns of the mailer renderer | |
expect(assigns(:user)).to eq user | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module MailerHelpers | |
def mail(method, *arguments) | |
@_mail_called = true | |
@_mailer_method = method | |
@_mailer_arguments = arguments | |
end | |
def mailer | |
raise "You must use mail(:method_name) before calling :mailer" unless @_mail_called | |
@mailer ||= described_class.send :new, @_mailer_method, *@_mailer_arguments | |
end | |
def response | |
raise "You must use mail(:method_name) before calling :response" unless @_mail_called | |
@response ||= mailer.message | |
end | |
def assigns(method) | |
raise "You must use mail(:method_name) before calling :assigns" unless @_mail_called | |
mailer.view_assigns.with_indifferent_access[method] | |
end | |
end | |
RSpec.configure do |config| | |
config.include MailerHelpers, type: :mailer | |
end |
Looks like this fixes it in Rails 5:
module MailerHelpers
def mail(method, *arguments)
@_mail_called = true
@_mailer_method = method
@_mailer_arguments = arguments
end
def mailer
raise "You must use mail(:method_name) before calling :mailer" unless @_mail_called
@mailer ||= described_class.new.tap do |mailer|
mailer.process(@_mailer_method, *@_mailer_arguments)
end
end
def response
raise "You must use mail(:method_name) before calling :response" unless @_mail_called
@response ||= mailer.message
end
def assigns(method)
raise "You must use mail(:method_name) before calling :assigns" unless @_mail_called
mailer.view_assigns.with_indifferent_access[method]
end
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This appears to be broken in Rails 5 😞