2014-07-23 18 views
6

Các lời chào!
Tôi đang cố gắng để thử nghiệm một phương pháp điều khiển với RSpec trông như thế này: Stubbing ActionMailer trong Rspec

def email_customer 
    @quote = Quote.find(params[:quote_id]) 
    hash = { 
    quote: @quote, 
    body: params[:email_body], 
    subject: params[:email_subject] 
    } 
    QuoteMailer.email_customer(hash).deliver 
    redirect_to edit_quote_path params[:quote_id] 
end 

Và spec tương ứng trông như thế này:

describe 'POST email_customer' do 
    let!(:quote) { create(:valid_quote) } 
    it 'assigns the quote and sends the customer an email' do 
    post :email_customer, quote_id: quote.id 
    expect(assigns(:quote)).to eq(quote) 
    expect(QuoteMailer).to receive(:email_customer).with(an_instance_of(Hash)).and_return(double('QuoteMailer', deliver: true)) 
    end 
end 

Khi thử nghiệm chạy mặc dù, tôi nhận được thông báo này:

Failure/Error: expect(QuoteMailer).to receive(:email_customer).with(an_instance_of(Hash)).and_return(double('QuoteMailer', deliver: true)) 
     (<QuoteMailer (class)>).email_customer(an instance of Hash) 
      expected: 1 time with arguments: (#<RSpec::Matchers::AliasedMatcher:0x0000000c2b1e28 @description_block=#<Proc:[email protected]/home/david/.rvm/gems/ruby-2.1.1/gems/rspec-expectations-3.0.0.beta2/lib/rspec/matchers.rb:231 (lambda)>, @base_matcher=#<RSpec::Matchers::BuiltIn::BeAnInstanceOf:0x0000000c2b1e50 @expected=Hash>>) 
      received: 0 times with arguments: (#<RSpec::Matchers::AliasedMatcher:0x0000000c2b1e28 @description_block=#<Proc:[email protected]/home/david/.rvm/gems/ruby-2.1.1/gems/rspec-expectations-3.0.0.beta2/lib/rspec/matchers.rb:231 (lambda)>, @base_matcher=#<RSpec::Matchers::BuiltIn::BeAnInstanceOf:0x0000000c2b1e50 @expected=Hash>>) 
    # ./spec/controllers/quotes_controller_spec.rb:28:in `block (3 levels) in <top (required)>' 

Tôi đã rải rác đặt câu lệnh trong suốt phương pháp điều khiển như chúng tôi sẽ là phương pháp email_customer vì vậy tôi biết rằng nó kết thúc bằng cách chạy khóa học của nó và sử dụng đúng phương pháp, nhưng tôi không chắc chắn tại sao nó không thành công. Tôi đoán một lỗi cú pháp ngu ngốc của nó mà tôi không chắc chắn về. Cảm ơn trước sự giúp đỡ của bạn!

+0

viết của bạn 'expect's _before_ 'cái post' –

+0

Ahh, trước đây tôi đã thử đặt cả hai dự đoán trước bài đăng, điều này đã cho tôi một lỗi liên quan đến việc chuyển nhượng @quote, nhưng với bài viết trước khi bài đăng hoạt động hoàn hảo! Cảm ơn ngài! nếu bạn làm cho rằng một câu trả lời tôi sẽ chấp nhận nó – davidicus

Trả lời

9

nhắn expect ations sẽ xuất hiện trước cuộc gọi đến phương thức mà bạn muốn kiểm tra, kể từ khi họ thực sự còn sơ khai được thông báo trong đối tượng:

describe 'POST email_customer' do 
    let!(:quote) { create(:valid_quote) } 
    it 'assigns the quote and sends the customer an email' do 
    expect(QuoteMailer).to receive(:email_customer).with(an_instance_of(Hash)).and_return(double('QuoteMailer', deliver: true)) 

    post :email_customer, quote_id: quote.id 

    expect(assigns(:quote)).to eq(quote) 
    end 
end 
+0

có 'mong đợi (gán (: quote)). để eq (quote)' trước khi bài dẫn tôi đến lỗi này: 'Thất bại/Lỗi: mong đợi (gán (: quote) đến eq (trích dẫn): dự kiến: # có: nil' – davidicus

+0

@davidicus - có thể đơn giản là nó không vượt qua? (giá trị nó nhận được là 'nil') –

+0

không bởi vì khi tôi di chuyển' mong đợi (gán (: quote)) .với eq (quote) 'to _after_ 'post: email_customer, quote_id: quote.id' nó vượt qua – davidicus

Các vấn đề liên quan