所以我的應用程式中有一個重試規則。我想使用 rspec 測驗它。
當我在不傳遞帳戶 ID 的情況下呼叫服務時,它應該給我 false,當傳遞帳戶 ID 時,它應該給我真。
我正在嘗試此代碼
options = {
email: '[email protected]'
}
expect_any_instance_of(PaymentService::CreatePayment).to receive(:call)
.with(options)
.and_return(false)
options[:account_id] = 12345
expect_any_instance_of(PaymentService::CreatePayment).to receive(:call)
.with(options)
.and_return(true)
但這對我不起作用。
uj5u.com熱心網友回復:
嘗試重寫為:
invalid_options = {
email: '[email protected]'
}
expect_any_instance_of(PaymentService::CreatePayment).to receive(:call)
.with(invalid_options)
.and_return(false)
valid_options = {
email: '[email protected]',
account_id: 12345
}
expect_any_instance_of(PaymentService::CreatePayment).to receive(:call)
.with(valid_options)
.and_return(true)
uj5u.com熱心網友回復:
所以我想出了一種方法來做到這一點..
expect_any_instance_of(PaymentService::CreatePayment).to receive(:call).twice do |_, _, options|
options[:account_id].present? ? true : false
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/357550.html
標籤:红宝石 ruby-on-rails-3 规格
