Skip to content

Commit 41628f8

Browse files
committed
Merge pull request rspec#1553 from rspec/readme_for_mailer
Add readme for mailer specs
2 parents 897d2a5 + 9a20df1 commit 41628f8

File tree

3 files changed

+110
-1
lines changed

3 files changed

+110
-1
lines changed

README.md

+31-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,37 @@ gem "capybara"
285285
```
286286

287287
For more information, see the [cucumber scenarios for feature
288-
specs](https://www.relishapp.com/rspec/rspec-rails/v/3-0/docs/feature-specs/feature-spec).
288+
specs](https://www.relishapp.com/rspec/rspec-rails/v/3-4/docs/feature-specs/feature-spec).
289+
290+
## Mailer specs
291+
292+
By default Mailer specs reside in the `spec/mailers` folder. Adding the metadata
293+
`:type => :mailer` to any context makes its examples be treated as mailer specs.
294+
295+
`ActionMailer::TestCase::Behavior` is mixed into your mailer specs.
296+
297+
```ruby
298+
require "rails_helper"
299+
300+
RSpec.describe Notifications, :type => :mailer do
301+
describe "notify" do
302+
let(:mail) { Notifications.signup }
303+
304+
it "renders the headers" do
305+
expect(mail.subject).to eq("Signup")
306+
expect(mail.to).to eq(["[email protected]"])
307+
expect(mail.from).to eq(["[email protected]"])
308+
end
309+
310+
it "renders the body" do
311+
expect(mail.body.encoded).to match("Hi")
312+
end
313+
end
314+
end
315+
```
316+
317+
For more information, see the [cucumber scenarios for mailer specs
318+
](https://relishapp.com/rspec/rspec-rails/v/3-4/docs/mailer-specs).
289319

290320
## View specs
291321

features/mailer_specs/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
By default Mailer specs reside in the `spec/mailers` folder. Adding the metadata
2+
`:type => :mailer` to any context makes its examples be treated as mailer specs.
3+
4+
A mailer spec is a thin wrapper for an ActionMailer::TestCase, and includes all
5+
of the behavior and assertions that it provides, in addition to RSpec's own
6+
behavior and expectations.
7+
8+
## Examples
9+
10+
require "rails_helper"
11+
12+
RSpec.describe Notifications, :type => :mailer do
13+
describe "notify" do
14+
let(:mail) { Notifications.signup }
15+
16+
it "renders the headers" do
17+
expect(mail.subject).to eq("Signup")
18+
expect(mail.to).to eq(["[email protected]"])
19+
expect(mail.from).to eq(["[email protected]"])
20+
end
21+
22+
it "renders the body" do
23+
expect(mail.body.encoded).to match("Hi")
24+
end
25+
end
26+
end
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
Feature: mailer spec
2+
3+
@rails_post_5
4+
Scenario: simple passing example
5+
Given a file named "spec/mailers/notifications_mailer_spec.rb" with:
6+
"""ruby
7+
require "rails_helper"
8+
9+
RSpec.describe NotificationsMailer, :type => :mailer do
10+
describe "notify" do
11+
let(:mail) { NotificationsMailer.signup }
12+
13+
it "renders the headers" do
14+
expect(mail.subject).to eq("Signup")
15+
expect(mail.to).to eq(["[email protected]"])
16+
expect(mail.from).to eq(["[email protected]"])
17+
end
18+
19+
it "renders the body" do
20+
expect(mail.body.encoded).to match("Hi")
21+
end
22+
end
23+
end
24+
"""
25+
When I run `rspec spec`
26+
Then the example should pass
27+
28+
@rails_pre_5
29+
Scenario: using URL helpers without default options
30+
Given a file named "config/initializers/mailer_defaults.rb" with:
31+
"""ruby
32+
# no default options
33+
"""
34+
And a file named "spec/mailers/notifications_spec.rb" with:
35+
"""ruby
36+
require 'rails_helper'
37+
38+
RSpec.describe Notifications, :type => :mailer do
39+
let(:mail) { Notifications.signup }
40+
41+
it "renders the headers" do
42+
expect(mail.subject).to eq("Signup")
43+
expect(mail.to).to eq(["[email protected]"])
44+
expect(mail.from).to eq(["[email protected]"])
45+
end
46+
47+
it 'renders the body' do
48+
expect(mail.body.encoded).to match("Hi")
49+
end
50+
end
51+
"""
52+
When I run `rspec spec`
53+
Then the examples should all pass

0 commit comments

Comments
 (0)