在rails功能测试中发送浏览器字符串

时间:2011-02-08 18:02:05

标签: ruby-on-rails functional-testing

应用程序应根据浏览器设置设置区域设置,但我没有意识到如何测试它。我不知道我可以提供哪些代码示例来解释,但测试是这样的:

def test_locale_settings
    get :index, {}, {:user_id => 1} # send browser settings to english
    assert_select '.nav .welcome', :text => 'Welcome'
    get :index, {}, {:user_id => 1} # send browser settings to spanish
    assert_select '.nav .welcome', :text => 'Bienvenido'
end

1 个答案:

答案 0 :(得分:1)

不确定这是否有效,但请尝试一下:

def test_locale_settings
    @request.env["HTTP_ACCEPT_LANGUAGE"] = "en"
    get :index, {}, {:user_id => 1} # send browser settings to english
    assert_select '.nav .welcome', :text => 'Welcome'

    @request.env["HTTP_ACCEPT_LANGUAGE"] = "es"
    get :index, {}, {:user_id => 1} # send browser settings to spanish
    assert_select '.nav .welcome', :text => 'Bienvenido'
end

当然,您的应用程序需要知道如何解析HTTP Accept Language标头的格式。