我的控制器有这段代码:
class MyController < App::BaseController
def login
redirect_to 'https://some.api.com/auth?params'
end
end
当然,我不想在测试中被重定向到外部网站。所以我决定添加 webmock gem 并添加:
WebMock.disable_net_connect!(allow_localhost: true)
对我的spec_helper.rb
.
在我的功能测试中,我有:
scenario 'stubbing external api' do
visit '/connect_to_some_external_api'
stub_request(:any, /https:\/\/some.api.com\/auth\//)
.to_return(status: 200, body: 'DUPA. A nawet DUPA123')
# this works as intended ->Net::HTTP.get('https://some.api.com/auth/')
click_link 'Get me to MyController#login'
end
当我打电话时,Net::HTTP.get('https://some.api.com/auth/')
我得到了回应"DUPA. A nawet DUPA123"
。这很酷!这意味着stub_request
有效。
问题是当用户单击指向MyController#get
重定向发生位置的链接时。它不会存根请求,只是让浏览器重定向到外部应用程序。
是否可以存根来自控制器的请求redirect_to
?如果是,那么如何?