使用路由断言帮助程序编写测试,我想将请求标头设置为Facebook Share bot
class RoutesTest < ActionController::TestCase
test 'route social share bot to bots controller' do
assert_recognizes({controller: 'bots', action: 'index'}, '/careers/1',
{ @request.headers['HTTP_USER_AGENT'] => 'facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)' })
end
end
测试记录实际识别的选项显示为
{
"controller"=>"site/pages",
"action"=>"home", "path"=>"careers/1",
"Rails Testing"=>"facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)"
}
这并不像我设置请求标头那样。我是否在这里正确设置用户代理标题有点困惑?
更新
通过ActionContoller :: TestCase文档,这应该有效
test 'route social share bot to bots controller' do
@request.user_agent = 'facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)'
assert_recognizes({controller: 'site/bots', action: 'index', path: 'careers/1'}, '/careers/1')
end
但是也没有设置用户代理,我也尝试了@request.env['HTTP_USER_AGENT']
风味