在升级后的 rails 5 应用程序中对控制器测试中的请求进行存根

Stubbing requests in controller tests in upgraded rails 5 application

在升级到 rails 5 之前,我们的应用使用

在控制器测试中成功地存根了 request.user_agent
allow(request).to receive(:user_agent).and_return 'Special Agent'

我注意到 stubbing now uses ActionDispatch::IntegrationTest 所以 我尝试使用 #capybara-user_agent gem 和

set_custom_user_agent('Special Agent')

在我的控制器测试中,但这也不起作用。但是,set_custom_user_agent 在 rails 4 和 rails 5 中的功能规范中确实有效。如何在 rails 5 中的控制器规范中存根用户代理?

您是否尝试在 before 块中调用 request.env['HTTP_USER_AGENT'] = 'agent'?我确定您可以在存根不起作用时修改 Rack 环境