Skip to content

Add readme for mailer specs #1553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,37 @@ gem "capybara"
```

For more information, see the [cucumber scenarios for feature
specs](https://www.relishapp.com/rspec/rspec-rails/v/3-0/docs/feature-specs/feature-spec).
specs](https://www.relishapp.com/rspec/rspec-rails/v/3-4/docs/feature-specs/feature-spec).

## Mailer specs

By default Mailer specs reside in the `spec/mailers` folder. Adding the metadata
`:type => :mailer` to any context makes its examples be treated as mailer specs.

`ActionMailer::TestCase::Behavior` is mixed into your mailer specs.

```ruby
require "rails_helper"

RSpec.describe Notifications, :type => :mailer do
describe "notify" do
let(:mail) { Notifications.signup }

it "renders the headers" do
expect(mail.subject).to eq("Signup")
expect(mail.to).to eq(["[email protected]"])
expect(mail.from).to eq(["[email protected]"])
end

it "renders the body" do
expect(mail.body.encoded).to match("Hi")
end
end
end
```

For more information, see the [cucumber scenarios for mailer specs
](https://relishapp.com/rspec/rspec-rails/v/3-4/docs/mailer-specs).

## View specs

Expand Down
26 changes: 26 additions & 0 deletions features/mailer_specs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
By default Mailer specs reside in the `spec/mailers` folder. Adding the metadata
`:type => :mailer` to any context makes its examples be treated as mailer specs.

A mailer spec is a thin wrapper for an ActionMailer::TestCase, and includes all
of the behavior and assertions that it provides, in addition to RSpec's own
behavior and expectations.

## Examples

require "rails_helper"

RSpec.describe Notifications, :type => :mailer do
describe "notify" do
let(:mail) { Notifications.signup }

it "renders the headers" do
expect(mail.subject).to eq("Signup")
expect(mail.to).to eq(["[email protected]"])
expect(mail.from).to eq(["[email protected]"])
end

it "renders the body" do
expect(mail.body.encoded).to match("Hi")
end
end
end
53 changes: 53 additions & 0 deletions features/mailer_specs/mailer_spec.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
Feature: mailer spec

@rails_post_5
Scenario: simple passing example
Given a file named "spec/mailers/notifications_mailer_spec.rb" with:
"""ruby
require "rails_helper"

RSpec.describe NotificationsMailer, :type => :mailer do
describe "notify" do
let(:mail) { NotificationsMailer.signup }

it "renders the headers" do
expect(mail.subject).to eq("Signup")
expect(mail.to).to eq(["[email protected]"])
expect(mail.from).to eq(["[email protected]"])
end

it "renders the body" do
expect(mail.body.encoded).to match("Hi")
end
end
end
"""
When I run `rspec spec`
Then the example should pass

@rails_pre_5
Scenario: using URL helpers without default options
Given a file named "config/initializers/mailer_defaults.rb" with:
"""ruby
# no default options
"""
And a file named "spec/mailers/notifications_spec.rb" with:
"""ruby
require 'rails_helper'

RSpec.describe Notifications, :type => :mailer do
let(:mail) { Notifications.signup }

it "renders the headers" do
expect(mail.subject).to eq("Signup")
expect(mail.to).to eq(["[email protected]"])
expect(mail.from).to eq(["[email protected]"])
end

it 'renders the body' do
expect(mail.body.encoded).to match("Hi")
end
end
"""
When I run `rspec spec`
Then the examples should all pass