Skip to content

Commit 6c7621e

Browse files
committed
extended specs and made them work
1 parent 8b300a0 commit 6c7621e

3 files changed

Lines changed: 37 additions & 19 deletions

File tree

app/controllers/mailer_controller.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class MailerController < ApplicationController
22
def new
33
@users = Array(params[:user])
4-
@emails = User.where(:username => @users).all.map(&:email);
4+
@emails = User.where(:username => @users).all.map(&:email)
55
@message = Message.new
66
end
77

@@ -14,8 +14,7 @@ def create
1414
end
1515
redirect_to users_index_path, :notice => "Email sent correctly"
1616
else
17-
notice = "There was an error"
18-
render :new
19-
end
17+
render :new, notice: "There was an error"
18+
end
2019
end
2120
end

app/models/message.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ class Message
44
key :subject, String
55
key :body, String
66
validates_presence_of :email, :body
7+
8+
def to_param
9+
{email: email, subject: subject, body: body}
10+
end
711
end

spec/controllers/mailer_controller_spec.rb

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,54 @@
66

77
describe "GET 'new' for a single user email" do
88
it "returns http success" do
9-
get 'new', user: Array(user)
9+
get :new, user: Array(user)
1010
response.should be_success
1111
end
1212
end
1313

1414
describe "GET 'new' for a diffusion" do
1515
it "returns http success" do
16-
get 'new', user: Array(users)
16+
get :new, user: Array(users)
1717
response.should be_success
1818
end
1919
end
2020

2121
describe "POST 'create' for a single user email" do
22-
it "returns http success" do
22+
before :each do
2323
@message = Fabricate.build(:message)
24-
post 'create', message: @message
25-
response.should be_success
26-
#We expect after send a message that the deliveries raise one
27-
expect { MessageMailer.new_message(@message, @message.email).deliver }.to change { ActionMailer::Base.deliveries.size }.by(1)
24+
end
25+
26+
it 'delivers the email' do
27+
expect {
28+
post :create, message: @message
29+
}.to change {ActionMailer::Base.deliveries.size}.by(1)
30+
end
31+
32+
describe 'delivered message' do
33+
before :each do
34+
post :create, message: @message
35+
end
36+
37+
it "returns http success" do
38+
response.should be_redirect
39+
end
40+
41+
it 'delivers the mail with the subject that we wanted to' do
42+
ActionMailer::Base.deliveries.last.subject.should == @message.subject
43+
end
44+
45+
it 'delivers the mail with the body that we wanted to' do
46+
ActionMailer::Base.deliveries.last.body.to_s.should match @message.body
47+
end
2848
end
2949
end
3050

3151
describe "POST 'create' for a diffusion" do
3252
it "returns http success" do
3353
@diffusion = Fabricate.build(:diffusion)
34-
post 'create', message: @diffusion
35-
response.should be_success
36-
37-
@before_send = ActionMailer::Base.deliveries.size
38-
@diffusion.email.each do |email|
39-
MessageMailer.new_message(@diffusion, email).deliver
40-
end
41-
ActionMailer::Base.deliveries.size.should eq @before_send + @diffusion.email.size
54+
expect {
55+
post 'create', message: @diffusion
56+
}.to change {ActionMailer::Base.deliveries.size}.by(@diffusion.email.size)
4257
end
4358
end
4459
end

0 commit comments

Comments
 (0)