10

Hiện tại tôi đặc tả điều khiển tôi có:Làm cách nào để viết kiểm tra bộ điều khiển Rspec để đảm bảo rằng một e-mail được gửi?

require 'spec_helper' 

describe CustomerTicketsController do 
    login_user 

    describe "POST /create (#create)" do 
    # include EmailSpec::Helpers 
    # include EmailSpec::Matchers 
    it "should deliver the sales alert email" do 
     # 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 

Trong điều khiển của tôi, tôi có:

# POST /customer_tickets 
    # POST /customer_tickets.xml 
    def create 
    respond_to do |format| 
     if @customer_ticket.save 
     CustomerMailer.sales_alert(@customer_ticket).deliver 
     format.html { redirect_to @customer_ticket, notice: 'Customer ticket was successfully 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 

thử nghiệm của tôi hiện đang sản xuất các đầu ra sau đây:

Failures: 

    1) CustomerTicketsController POST /create (#create) should deliver the 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: "[email protected]", phone: "555-5555", fax: nil, country: nil, address1: "555 Rodeo Dr.", address2: nil, city: "Beverly Hills", state: "CA", postcode: "90210", question: "The answer to the universe is 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 the sales alert email 

Thank bạn để tìm kiếm.

Trả lời

18

Bạn có thể kiểm tra ActionMailer :: Base.deliveries.count để đảm bảo nó đã tăng thêm 1.

Something như thế này (chưa được kiểm tra)

expect {custom_mailer.deliver}.to change { ActionMailer::Base.deliveries.count }.by(1) 
+2

Tôi đặt 'post: create,: customer_ticket => customer_ticket_attributes' bên trong khối mong đợi và dường như đang thực hiện công việc. – jklina

1

Các mocks bạn đang thiết lập không khớp với những gì đang thực sự được thực thi. Bạn phải thực sự gọi số deliver trên mô hình mà bạn đang tạo.

Something như

message = mock('Message') 
CustomerMailer.should_receive(:sales_alert).and_return(message) 
message.should_receive(:deliver) 

nên vượt qua.

Nếu bạn muốn kiểm tra những gì được chuyển đến sales_alert những gì bạn đang làm sẽ không hoạt động - bản ghi hoạt động chỉ bao giờ so sánh các đối tượng theo bình đẳng khóa chính sao cho vé khách hàng bạn đang tạo trong thông số sẽ không bằng nhau cho người được tạo trong bộ điều khiển của bạn.

Một điều bạn có thể làm là

CustomerMailer.should_receive(:sales_alert) do |arg| 
    ... 
end.and_return(message) 

rspec sẽ mang lại bất cứ điều gì sales_alert được gọi với và bạn có thể làm bất cứ điều gì để kiểm tra bạn muốn

Một cách khác để làm việc đó để là còn sơ khai ra CustomerTicket.new, vì vậy sau đó bạn kiểm soát nội dung trả lại, ví dụ:

mock_ticket = mock(CustomerTicket) 
CustomerTicket.should_receive(:new).with(customer_ticket_attributes).and_return(mock_ticket) 
mock_ticket.should_receive(:save).and_return(true) 
CustomerMailer.should_receive(:sales_alert).with(mock_ticket).and_return(message) 

Cuối cùng, bạn có thể quyết định gửi cảnh báo này thực sự thuộc về Ví dụ phương thức CustomerTicket trong trường hợp đặc điểm điều khiển của bạn chỉ có thể kiểm tra xem phương thức send_sales_alert đã được gọi trên vé hay chưa.

0

Đây là cách làm thế nào để kiểm tra rằng Mailer là được gọi với các đối số đúng.

delivery = double 
expect(delivery).to receive(:deliver_now).with(no_args) 

expect(CustomerMailer).to receive(:sales_alert) 
    .with(customer_ticket) 
    .and_return(delivery) 
Các vấn đề liên quan