Skip to content

Instantly share code, notes, and snippets.

@mergulhao
Created August 30, 2012 03:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mergulhao/3521922 to your computer and use it in GitHub Desktop.
Save mergulhao/3521922 to your computer and use it in GitHub Desktop.
# -*- encoding : utf-8 -*-
require "spec_helper"
describe Notification do
describe "new_question" do
let(:user) { Factory.create(:user, name: 'Dart Vader', email: 'dart@example.com') }
let(:category) { Factory.create(:category, name: 'Saúde') }
let(:owner) { Factory.create(:user, name: 'Jaspion') }
let(:question) { Factory.create(:question, user: owner, category: category) }
let(:mailer) { Notification.new_question(question.id, user.id) }
it { lambda { Notification.new_question(question.id, user.id) }.should_not raise_error }
it { mailer.subject.should == %{[Hintask] Nova pergunta da sua área!} }
it { mailer.body.to_s.should == read_mail("notification/new_question", question.id) }
it { mailer.header['From'].to_s.should == "Hintask <contato@hintask.com>" }
it { mailer.header['To'].to_s.should == "Dart Vader <dart@example.com>" }
it { mailer.from.size.should == 1 }
it { mailer.to.size.should == 1 }
it { mailer.cc.should be_nil }
it { mailer.bcc.should be_nil }
it { mailer.multipart?.should be_false }
it { mailer.charset.should == "UTF-8" }
it { mailer.content_type.should == "text/html; charset=UTF-8" }
it {
expect {
Notification.new_question(question.id, user.id).deliver
}.to change(ActionMailer::Base.deliveries,:size).by(1)
}
end
private
def read_mail(name, id = nil)
filename = Rails.root.join('spec','fixtures',"#{name}.html")
@body = mailer.body.to_s
@body.gsub!(id.to_s, "*|ID|*") unless id.nil?
File.open(filename, 'w') {|f| f.write(@body) } unless File.exists?(filename)
File.read(filename).gsub("*|ID|*", id.to_s)
end
end
# -*- encoding : utf-8 -*-
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
describe Devise::Mailer do
before(:each) do
I18n.locale = :'pt-BR'
end
describe "reset_password_instructions" do
before(:each) do
@user = Factory.create(:user, :name => 'Nome do Usuario', :email => 'emaildousuario@example.com')
end
it "should render successfully" do
lambda { Devise::Mailer.reset_password_instructions(@user) }.should_not raise_error
end
describe "rendered without error" do
before(:each) do
@mailer = Devise::Mailer.reset_password_instructions(@user)
end
it "should set correct subject" do
@mailer.subject.should == "Instruções para troca de senha"
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 == "emaildousuario@example.com"
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 CC on recipients address" do
@mailer.bcc.should be_nil
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 == %{"Amigo não se Compra" <contato@amigonaosecompra.com.br>}
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
@mailer.body.to_s.should == read_mail("reset_password_instructions")
end
it "should set correct content type" do
@mailer.content_type.should == "text/html; charset=UTF-8"
end
it "should deliver successfully" do
lambda { Devise::Mailer.reset_password_instructions(@user).deliver }.should_not raise_error
end
describe "and delivered" do
it "should be added to the delivery queue" do
lambda { Devise::Mailer.reset_password_instructions(@user).deliver }.should change(ActionMailer::Base.deliveries,:size).by(1)
end
end
end
end
private
def read_mail(name)
filename = Rails.root.join('spec','fixtures','devise','mailer',"#{name}.html")
File.open(filename, 'w') {|f| f.write(@mailer.body) } unless File.exists?(filename)
File.read(filename)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment