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

发布于 2024-10-16 11:21:21 字数 385 浏览 3 评论 0原文

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

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

The application should set the locale based on browser settings, but I don't realize how to test it. I don't know what code sample could I provide to explain, but the test is something like this:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戏舞 2024-10-23 11:21:21

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

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 标头的格式。

Not sure if this works, but give it a try:

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

Of course your application will need to know how to parse the format of the HTTP Accept Language header.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文