ruby on rails - How do I write an Rspec controller test that makes sure an e-mail is sent? -


currently in controller spec have:

require 'spec_helper'  describe customerticketscontroller   login_user    describe "post /create (#create)"     # include emailspec::helpers     # include emailspec::matchers     "should deliver sales alert email"       # expect       customer_ticket_attributes = factorygirl.attributes_for(:customer_ticket)       customer_mailer = mock(customermailer)       customer_mailer.should_receive(:deliver).         with(customerticket.new(customer_ticket_attributes))       # when       post :create, :customer_ticket => customer_ticket_attributes     end   end end 

in controller have:

  # post /customer_tickets   # post /customer_tickets.xml   def create     respond_to |format|       if @customer_ticket.save         customermailer.sales_alert(@customer_ticket).deliver         format.html { redirect_to @customer_ticket, notice: 'customer ticket created.' }         format.xml { render xml: @customer_ticket, status: :created, location: @customer_ticket }       else         format.html { render action: "new" }         format.xml { render xml: @customer_ticket.errors, status: :unprocessable_entity }       end     end   end 

my test produces following output:

failures:    1) customerticketscontroller post /create (#create) should deliver sales alert email      failure/error: customer_mailer.should_receive(:deliver).        (mock customermailer).deliver(#<customerticket id: nil, first_name: "firstname1", last_name: "lastname1", company: nil, referral: nil, email: "firstname1@example.com", phone: "555-5555", fax: nil, country: nil, address1: "555 rodeo dr.", address2: nil, city: "beverly hills", state: "ca", postcode: "90210", question: "the answer universe 4.", type: nil, status: nil, priority: nil, number: nil, cs_rep_id: nil, created_at: nil, updated_at: nil>)            expected: 1 time            received: 0 times      # ./spec/controllers/customer_ticket_controller_spec.rb:13:in `block (3 levels) in <top (required)>'  finished in 0.52133 seconds 1 example, 1 failure  failed examples:  rspec ./spec/controllers/customer_ticket_controller_spec.rb:9 # customerticketscontroller post /create (#create) should deliver sales alert email 

thank looking.

you check actionmailer::base.deliveries.count ensure has incremented 1.

something (untested)

expect {custom_mailer.deliver}.to change { actionmailer::base.deliveries.count }.by(1) 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -