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!
viết của bạn 'expect's _before_ 'cái post' –
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