2017-03-02 28 views
0

我有一个类可以调用另一个对象的命令,我试图在RSpec中测试它。在Rspec中模拟多个方法,其中第一个方法需要参数

SendWithUsInviteeMailer.send_invite(invitee_id).deliver

我想写的东西是这样的:

expect(SendWithUsInviteeMailer).to receive_message_chain(:send_invite, :deliver).with(invitee.id)

其中invitee.id被作为参数传递给第一种方法。我如何正确测试它?有没有使用消息链的方法?

回答

1

我可能存根send_invite方法和检查交货,像这样:

# config/environments/test.rb 
config.action_mailer.delivery_method = :test 

而在你的规格:

before do 
    ActionMailer::Base.deliveries.clear 
    allow(SendWithUsInviteeMailer).to receive(:send_invite).and_call_original 
end 

it 'sends out the right email' 
    expect(SendWithUsInviteeMailer).to receive(:send_invite).with(invitee.id) 
    # perform the action to be tested 
    expect(ActionMailer::Base.deliveries.count).to eq 1 
end 
+0

是对SendWithUs梅勒是一种行动的邮包?测试设置中发生了什么?这可能与我不理解actionmailer有关?它有什么作用? – Jwan622

相关问题