Saturday 7 May 2011

How do we write the rspec for mailers……?

What is RSpec?
RSpec is a behavior driven development (BDD) framework for the Ruby programming language
It contains its own mocking framework that is fully integrated into the framework based upon JMock.
How do we start an RSpec test for our rails application?
step 1:  In your Gemfile add the given lines
group :development, :test do
gem “capybara”, “>= 0.3.9″
gem “rspec-rails”, “>= 2.0.0.beta.12″
gem “shoulda”
gem ‘webrat’, ‘>=0.7.2.beta.1′
gem “email_spec”
end
Next run bundle install command in your console.
step 2:    After that bundle install write in your console
$ rails generate rspec:install
rake db:migrate
step 3: To run your rspec file by using following command
rake spec
or
rspec spec/
How to write the rspec for mailers :
     mailer file:  (ProjectMailer)
def verify_secondary_email(email,code)
@url=verify_secondary_email_url(:verification_code=>code)
mail(:to=>email, :subject=>”Verify Your Email Address on Mocha”,:content_type=>”text/html”)
end
     corresponding Rspec
describe ProjectMailer do
before(:each) do
old_project=”Cothinkit”
new_project=”Mocha”
user=mock(“user”,:email=>”ramprabu@gmail.com”)
to_user = mock(“user”, :email =>”jimmy_bean@yahoo.com”)
@email = ProjectMailer.project_renamed(user,old_project,new_project,to_user)
end
it “should have the correct subject” do
assert @email.should have_subject(“Cothinkit Project Has Been Renamed to Mocha”)
end
it “should delivered successfully” do
@email.should deliver_to(“jimmy_bean@yahoo.com”)
end
it “should contain the mail body” do
@email.should have_body_text(“ramprabu@gmail.com has renamed Cothinkit to Mocha.<br/><br/> –<br/> Powered by GetMocha.com”)
end
end