如何更改 RSpec 的存根
How to change stub for RSpec
我有一个存根 returns a 422 for this test。这是为了生成一个模态,询问用户是否确定要继续。当他们单击“是,继续”时,我希望存根为 return 200 并进行测试以将用户带回主页。我怎么能那样做?当我尝试放入我想要的存根时,似乎最后写入的存根是被使用的存根。好像我不能两者兼顾。似乎当 save-refund-confirmation-button 被点击时,422 存根被第二次调用。我要使用 200 存根。
scenario 'successfully saves refund when user clicks yes continue on similar refund', js: true do
save_payer_refund_success_request = stub_request(:post, /remittances/).to_return(status: 200)
save_payer_refund_fail_request = stub_request(:post, /remittances/).to_return(status:422, body: {error: "SIMILAR_REMITTANCE"}.to_json)
visit record_refund_check_by_payer_remittances_path
#Test fils out the form here
page.find_by_id('refund_record').click
wait_for_ajax
expect(save_payer_refund_fail_request).to have_been_made # <--- This works
expect(page).to have_content 'A refund from this payer with this number with the amount of $' + @refundTotalAmount + ' and date of ' + @EFTCheckDate + ' already exists in Value-Based Reimbursement. Are you sure you want to record this refund?'
page.find_by_id('save-refund-confirmation-button').click
wait_for_ajax
expect(save_payer_refund_success_request).to have_been_made # <--- Fails here
expect(page).to have_content 'Refund check ' + @checkNumber + ' to payer has been saved.'
如果您想要 stub_request
到 return 对同一匹配端点的连续调用的不同响应,那么您需要在一次 stub_request
调用中指定所有这些响应 - https://github.com/bblimke/webmock#multiple-responses-for-repeated-requests
my_request = stub_request(:post, /remittances/).
to_return(status: 200).then.
to_return(status:422, body: {error: "SIMILAR_REMITTANCE"}.to_json)
...
expect(my_request).to have_been_requested.times(2)
注意:这只是对您的 app/tests 发出的请求进行存根(所以我假设您的应用会在内部回调到它自己的 API)——这实际上并不是对任何人发出的请求进行存根您与 Capybara
一起使用的浏览器
我有一个存根 returns a 422 for this test。这是为了生成一个模态,询问用户是否确定要继续。当他们单击“是,继续”时,我希望存根为 return 200 并进行测试以将用户带回主页。我怎么能那样做?当我尝试放入我想要的存根时,似乎最后写入的存根是被使用的存根。好像我不能两者兼顾。似乎当 save-refund-confirmation-button 被点击时,422 存根被第二次调用。我要使用 200 存根。
scenario 'successfully saves refund when user clicks yes continue on similar refund', js: true do
save_payer_refund_success_request = stub_request(:post, /remittances/).to_return(status: 200)
save_payer_refund_fail_request = stub_request(:post, /remittances/).to_return(status:422, body: {error: "SIMILAR_REMITTANCE"}.to_json)
visit record_refund_check_by_payer_remittances_path
#Test fils out the form here
page.find_by_id('refund_record').click
wait_for_ajax
expect(save_payer_refund_fail_request).to have_been_made # <--- This works
expect(page).to have_content 'A refund from this payer with this number with the amount of $' + @refundTotalAmount + ' and date of ' + @EFTCheckDate + ' already exists in Value-Based Reimbursement. Are you sure you want to record this refund?'
page.find_by_id('save-refund-confirmation-button').click
wait_for_ajax
expect(save_payer_refund_success_request).to have_been_made # <--- Fails here
expect(page).to have_content 'Refund check ' + @checkNumber + ' to payer has been saved.'
如果您想要 stub_request
到 return 对同一匹配端点的连续调用的不同响应,那么您需要在一次 stub_request
调用中指定所有这些响应 - https://github.com/bblimke/webmock#multiple-responses-for-repeated-requests
my_request = stub_request(:post, /remittances/).
to_return(status: 200).then.
to_return(status:422, body: {error: "SIMILAR_REMITTANCE"}.to_json)
...
expect(my_request).to have_been_requested.times(2)
注意:这只是对您的 app/tests 发出的请求进行存根(所以我假设您的应用会在内部回调到它自己的 API)——这实际上并不是对任何人发出的请求进行存根您与 Capybara
一起使用的浏览器