27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Receiving emails<br />

The final step for this spec is to actually parse the thing using the Receiver class,<br />

and to check that it changes the related ticket’s comment count by 1:<br />

lambda { Receiver.parse(mail) }.should(<br />

change(comment.ticket.comments, :count).by(1)<br />

)<br />

The spec/mailers/receiver_spec.rb should now look like the following listing.<br />

require 'spec_helper'<br />

describe Receiver do<br />

it "parses a reply from a comment update into a comment" do<br />

comment = Factory(:comment)<br />

ticket = comment.ticket<br />

comment_email = ActionMailer::Base.deliveries.last<br />

user = Factory(:user)<br />

mail =<br />

C Create new email<br />

Mail.new(:from => user.email,<br />

:subject => "Re: #{comment_email.subject}",<br />

:body => %Q{This is a brand new comment<br />

#{comment_email.default_part_body}<br />

},<br />

:to => comment_email.from)<br />

end<br />

end<br />

Listing 12.15 spec/mailers/receiver_spec.rb<br />

lambda { Receiver.parse(mail) }.should(<br />

change(ticket.comments, :count).by(1)<br />

)<br />

ticket.comments.last.text.should eql("This is a brand new comment")<br />

In this spec, you build a comment and reference the ticket for it. By creating a comment,<br />

there will be an email going out that you can access using Action-<br />

Mailer::Base.deliveries.last B because ActionMailer::Base.delivery_method<br />

is set to :test. Using this email, you can compile a new email using Mail.new C 14 and<br />

passing in some values using the original email’s methods. One of these values—<br />

:body—is way more important than the others. For this value, you want to take the<br />

original content of the email and then above it put the new comment text. You use<br />

default_part_body D. From this new text, a comment should be created. That’s<br />

exactly what you assert on the final few lines of this example by using RSpec’s change<br />

E method. On these final lines, you only want to make sure that the comments count<br />

has increased by one and that the latest comment has the text “This is a brand new<br />

comment.”<br />

14 This functionality is provided by the mail gem, on which Action Mailer depends.<br />

B Access email<br />

D<br />

343<br />

Insert original<br />

email content<br />

E Add 1 to comment count

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!