2014-07-23 26 views
6

Salutations!
Şöyle RSpec ile kontrolör yöntemi sınamak çalışıyorum: Rspec'de Stubbing ActionMailer

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 

Ve karşılık gelen spec şöyle görünür: Test olsa çalıştığında

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 

, ben olsun Bu ileti:

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)>' 

Denetleyicinin yöntemi boyunca ifadeleri pes ettiğimiz gibi dağıttım email_customer yöntemi olarak, bu yüzden kursunu yürütmeyi ve doğru yöntemi kullanmanın bittiğini biliyorum, ancak bunun neden başarısız olduğundan emin değilim. Bundan emin olmadığım aptal bir sözdizimi hatası tahmin ediyorum. Yardımın için şimdiden teşekkürler! aslında nesne mesajı saplama beri

+0

yazmak senin 'expect's' –

+0

post' _before_ ahh, daha önce her iki beklenti de yazmadan önce denedim, bu da bana @quote'un atanmasıyla ilgili bir hata verdi, ama yazıdan önce saplama ile kusursuz çalıştı! Teşekkürler bayım! Eğer bir cevap yaparsanız ben – davidicus

cevap

9

Mesaj expect ations, önce test etmek istediğiniz yönteme çağrısı görünmelidir:

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

kabul bekliyoruz (atama (: alıntı)) eq (alıntı) 'e göndermeden önce bu hata bana yol açar: ' Arıza/Hata: bekliyor (atar (alıntı: (alıntı) e) (alıntı): beklenen: # got: nil' – davidicus

+0

@davidicus - sadece geçemediği olabilir mi? (aldığı değer "nil" dir) –

+0

hayır çünkü ben hareket ettiğim zaman (atama (atar) (alıntı) (atar): _after_ 'post: email_customer, quote_id: quote.id' – davidicus

İlgili konular