RSpec:我们可以将期望配置为 return 两个不同的值吗?我想测试重试机制
RSpec: Can we configure an expectation to return two different value? I want to test retry mechanism
所以我的应用程序中有一个重试规则。我想使用 rspec.
进行测试
当我在没有传递帐户 ID 的情况下调用服务时,它应该给我 false,而当传递帐户 ID 时,它应该给我 true。
我正在尝试此代码
options = {
email: 'dev@dev.com'
}
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)
但这对我不起作用。
尝试重写为:
invalid_options = {
email: 'dev@dev.com'
}
expect_any_instance_of(PaymentService::CreatePayment).to receive(:call)
.with(invalid_options)
.and_return(false)
valid_options = {
email: 'dev@dev.com',
account_id: 12345
}
expect_any_instance_of(PaymentService::CreatePayment).to receive(:call)
.with(valid_options)
.and_return(true)
所以我想出了一个办法..
expect_any_instance_of(PaymentService::CreatePayment).to receive(:call).twice do |_, _, options|
options[:account_id].present? ? true : false
end
所以我的应用程序中有一个重试规则。我想使用 rspec.
进行测试当我在没有传递帐户 ID 的情况下调用服务时,它应该给我 false,而当传递帐户 ID 时,它应该给我 true。
我正在尝试此代码
options = {
email: 'dev@dev.com'
}
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)
但这对我不起作用。
尝试重写为:
invalid_options = {
email: 'dev@dev.com'
}
expect_any_instance_of(PaymentService::CreatePayment).to receive(:call)
.with(invalid_options)
.and_return(false)
valid_options = {
email: 'dev@dev.com',
account_id: 12345
}
expect_any_instance_of(PaymentService::CreatePayment).to receive(:call)
.with(valid_options)
.and_return(true)
所以我想出了一个办法..
expect_any_instance_of(PaymentService::CreatePayment).to receive(:call).twice do |_, _, options|
options[:account_id].present? ? true : false
end