Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@olivierlacan
Created June 8, 2014 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olivierlacan/a45fa9e74d31f4970964 to your computer and use it in GitHub Desktop.
Save olivierlacan/a45fa9e74d31f4970964 to your computer and use it in GitHub Desktop.
RSpec matcher for mandrill_mailer gem that allows you to check the main CONTENT merge var for a specific string using expect(mailer).to include_merge_var_content('my string')
# Public: Matcher for asserting specific merge vars content contains something.
#
# merge_var_key - Key of the merge var whose content will be checked
# expected_data - Data to look for in the specified merge var key.
#
# WelcomeMailer is an instance of MandrillMailler::TemplateMailer
#
# let(:user) { FactoryGirl.create(:user) }
# let(:mailer) { WelcomeMailer.welcome_registered(user) }
# it 'should have the correct data' do
# mailer.should have_merge_data('USER_EMAIL' => user.email)
# end
#
# Returns true or an error message on failure
#
RSpec::Matchers.define :include_merge_var_content do |expected_data|
match do |actual|
has_match = false
matches = merge_vars_from(actual).find do |hash|
hash['name'] == 'CONTENT'
end
has_match = matches['content'].include?(expected_data)
has_match
end
failure_message_for_should do |actual|
<<-MESSAGE.strip_heredoc
Expected merge variables: #{merge_vars_from(actual).inspect} to include content: #{expected_data}.
MESSAGE
end
failure_message_for_should_not do |actual|
<<-MESSAGE.strip_heredoc
Expected merge variables: #{merge_vars_from(actual).inspect} to not include content: #{expected_data}.
MESSAGE
end
description do
"include merge var content: #{expected_data}"
end
def merge_vars_from(mailer)
# Merge vars are in format:
# [{"name"=>"USER_EMAIL", "content"=>"zoila@homenick.name"},{"name"=>"USER_NAME", "content"=>"Bob"}]
mailer.data['message']['global_merge_vars']
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment