由于查询参数包括指定语言的本地,所有使用辅助方法(例如user_path,usre_url)的测试都将失败

问题描述 投票:0回答:1

我正在使用Ruby on Rails构建Web应用程序。

我最近将应用程序国际化。如果URL具有查询参数locale=en,则应用中的所有文本均为英语。

在我的测试代码中,我检查了网络上的所有链接,从而编写了许多集成测试代码,如下所示:

test "should access all links" do

    :

    get root_path
    assert_select "a[href=?]", root_path, count: 2
    assert_select "a[href=?]", help_path

    assert_select "a[href=?]", users_path
    assert_select "a[href=?]", user_path(@user)
    assert_select "a[href=?]", edit_user_path(@user)
    assert_select "a[href=?]", logout_path

    :
end

但是,由于我进行了应用程序的国际化,因此所有测试都会失败,因为默认情况下URL具有locale=en

我知道如果我像下面那样更改代码,所有测试都将通过,但是我认为这不是一个聪明的解决方案。

test "should access all links" do

    :

    get root_path
    assert_select "a[href=?]", "/locale=en", count: 2
    assert_select "a[href=?]", "/help?locale=en"

    assert_select "a[href=?]", "/users?locale=en"
    assert_select "a[href=?]", "/users/#{@user}?locale=en" user_path(@user)
    assert_select "a[href=?]", "/users/#{@user}/edit?locale=en"
    assert_select "a[href=?]", "/logout?locale=en"

    :
end

如果您知道更聪明的修复方法,请写下来。

ruby ruby-on-rails-5
1个回答
0
投票

我会选择类似的东西:

    assert_select "a[href=?]", root_path(locale: en), count: 2
    assert_select "a[href=?]", help_path(locale: en)

    assert_select "a[href=?]", users_path(locale: en)
    assert_select "a[href=?]", user_path(@user, locale: en)
    assert_select "a[href=?]", edit_user_path(@user, locale: en)
    assert_select "a[href=?]", logout_path(locale: en)
© www.soinside.com 2019 - 2024. All rights reserved.