Skip to content

Instantly share code, notes, and snippets.

@rafaelp
Created June 8, 2011 13:32
Show Gist options
  • Save rafaelp/1014417 to your computer and use it in GitHub Desktop.
Save rafaelp/1014417 to your computer and use it in GitHub Desktop.
Exemplo de como testar Mailers com RSpec
require "spec_helper"
describe Notification do
describe "idea created" do
before(:each) do
@idea = Idea.new({
:author_name => 'Nome do Usuario',
:author_email => '[email protected]',
})
@idea.stub(:id).and_return(98765)
end
it "should render successfully" do
lambda { Notification.idea_created(@idea) }.should_not raise_error
end
describe "rendered without error" do
before(:each) do
@mailer = Notification.idea_created(@idea)
end
it "should set correct subject" do
@mailer.subject.should == "Sua Idea foi Publicada."
end
it "should set correct header To" do
# Ele não coloca as aspas por que não receonhece nenhum caracter UTF-8 (acento)
@mailer.header['To'].to_s.should == "Nome do Usuario <[email protected]>"
end
it "should not have more than one to address" do
@mailer.to.size.should == 1
end
it "should consider CC on recipients address" do
@mailer.cc.should be_nil
end
it "should consider BCC on recipients address" do
@mailer.bcc.should == ["[email protected]"]
end
it "should set correct header From" do
# Ele coloca as aspas por que reconhece que existe um caracter UTF-8 (acento)
@mailer.header['From'].to_s.should == "Startup DEV <[email protected]>"
end
it "should not have more than one from address" do
@mailer.from.size.should == 1
end
it "should be multipart" do
@mailer.multipart?.should be_false
end
it "should set correct charset" do
@mailer.charset.should == "UTF-8"
end
it "should set correct body for text html" do
body = @mailer.body
filename = Rails.root.join('spec','fixtures','mailers','idea_created.html')
File.open(filename, 'w') {|f| f.write(body) } unless File.exists?(filename)
body.should == File.read(filename)
end
it "should set correct content type" do
@mailer.content_type.should == "text/html; charset=UTF-8"
end
it "should deliver successfully" do
lambda { Notification.idea_created(@idea).deliver }.should_not raise_error
end
describe "and delivered" do
it "should be added to the delivery queue" do
lambda { Notification.idea_created(@idea).deliver }.should change(ActionMailer::Base.deliveries,:size).by(1)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment