所以我的應用程式中有一個重試規則。我想使用 rspec 進行測驗。
當我在沒有傳遞帳戶 ID 的情況下呼叫服務時,它應該給我 false,而當通過帳戶 ID 時,它應該給我 true。
我正在嘗試這段代碼
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/qukuanlian/416231.html
標籤:
上一篇:嘗試設定路由路徑,但沒有路由匹配
