EmailSpectacular
High-level email spec helpers for acceptance, feature and request tests.
What EmailSpectacular is
Expressive email assertions that let you succinctly describe when emails should and should not be sent.
What EmailSpectacular is NOT
A library for low-level or unit-testing of ActionMailers.
Installation
Add this line to your application's Gemfile:
group :test do
gem 'email_spectacular', require: false
end
Add email_spectacular
to your spec/rails_helper.rb
require 'email_spectacular'
# ...
RSpec.configure do |config|
# ...
email_spectacular_spec_types = %i[acceptance feature request]
config.after(:each, type: email_spectacular_spec_types) do
# Clear emails between specs
clear_emails
end
email_spectacular_spec_types.each do |spec_type|
# Include email spectacular syntax in rspec tests
config.include EmailSpectacular::RSpec, type: spec_type
end
end
And then execute:
bundle install
Usage
Email receiver address
It's possible to assert an email was sent to one or more or more addresses using the following format:
expect(email).to have_been_sent.to('[email protected]')
Email sender address
Similarly, you can assert an email was sent from an address:
expect(email).to have_been_sent.from('[email protected]')
Email subject
You can assert an email's subject:
expect(email).to have_been_sent.with_subject('Welcome!')
Email body
You can assert the body of an email by text:
expect(email).to have_been_sent.with_text('Welcome, [email protected]')
Or using a selector on the email's HTML:
expect(email).to have_been_sent.with_selector('#password')
Or look for links:
expect(email).to have_been_sent.with_link('www.site.com/onboarding/1')
Or images:
expect(email).to have_been_sent.with_image('www.site.com/assets/images/welcome.png')
Chaining assertions
You can chain any combination of the above that you want for ultra specific assertions:
expect(email).to have_been_sent
.to('[email protected]')
.from('[email protected]')
.with_subject('Welcome!')
.with_text('Welcome, [email protected]')
.with_selector('#password').and('#username')
.with_link('www.site.com/onboarding/1')
.with_image('www.site.com/assets/images/welcome.png')
You can also chain multiple assertions of the the same type with the and
method:
expect(email).to have_been_sent
.with_text('Welcome, [email protected]').and('Thanks for signing up')
Asserting emails are NOT sent
The have_sent_email
assertion works with the negative case as well:
expect(email).to_not have_been_sent.with_text('Secret token')
Clearing emails
Emails can be cleared at any point by calling clear_emails
in your tests. This is helpful when you are testing a user workflow that may trigger multiple emails.
If you followed in installation steps above, emails will automatically be cleared between each spec.
Test suite
email_spectacular
comes with close-to-complete test coverage. You can run the test suite as follows:
rspec
Contributing
- Fork it ( https://github.com/greena13/email_spectacular/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request