Rails - 为什么我在集成测试中得到'你被重定向'?

时间:2016-12-13 09:31:38

标签: ruby-on-rails redirect integration-testing

在为Rails 5应用程序编写集成测试时,我遇到了臭名昭着的“你正被重定向”页面,这对我来说并不明显。有两个高度相似的测试:

test "GETtting correct activation link on an already activated user gives error message and redirects to root url" do
    # GIVEN a non-yet-registered email address
    email_address = "tester@testing.net"
    # GIVEN the sign-up page has been displayd
    get signup_path
    # GIVEN new user is created
    post signup_path, params: { user: { email: email_address, email_confirmation: email_address, password: "testpassword", password_confirmation: "testpassword" } }
    # GIVEN the URI from activation email
    activation_uri = URI.extract(ActionMailer::Base.deliveries.last.text_part.body.encoded)[0]
    # GIVEN the URI's been used and the user is already activated
    get activation_uri
    # WHEN reading back the newly activated user
    activated_user = User.find_by_email(email_address)
    # EXPECT the user to be activated
    assert activated_user.activated?
    # WHEN using the activation link on an already activated user
    get activation_uri
    # EXPECT redirection to root path
    assert_redirected_to root_url
    follow_redirect!
    # EXPECT flash message
    assert_not flash.empty?
    # EXPECT rendered page to contain activation error information
    assert_select 'div#flash div h5', text: I18n.translate('users.activate.error')
end

正确完成,下一个完成:

test "GETtting incorrect activation hash on a non-activated user gives error message and redirects to root url" do
    # GIVEN a non-yet-registered email address
    email_address = "tester@testing.net"
    # GIVEN the sign-up page has been displayd
    get signup_path
    # GIVEN new user is created
    post signup_path, params: { user: { email: email_address, email_confirmation: email_address, password: "testpassword", password_confirmation: "testpassword" } }
    # WEHN GETting the activation URI with invalid activation hash
    activation_uri = "http://localhost:3000/account_activations/waTbfcCoZoPTBEIcewsl8Q/edit?email=#{ERB::Util.url_encode(email_address)}"
    get activation_uri
    # EXPECT redirection to root path
    assert_redirected_to root_url
    follow_redirect!
    # EXPECT flash message
    assert_not flash.empty?
    # EXPECT rendered page to contain activation error information ('You are being redirected' rendered here)
    assert_select 'div#flash div h5', text: I18n.translate('users.activate.error')
end

在最后一个断言上失败了因为“你被重定向”正在呈现而不是我期望呈现的页面。在这两种情况下,我使用follow_redirect!,第一个使用,而第二个不使用。第二次测试中的静态URL是正确的。它只使用有效但非关联的哈希而不是预期的哈希。在控制器中有简单的

flash[:error] = "#{t'users.activate.error'}"
redirect_to root_url

在两种情况下(相同的方法)。我收到了正确的302响应代码和正确的重定向网址。在浏览器中手动执行相同的测试时,将呈现正确的页面。在进行测试时,我会在第二次测试中得到“你正在......”。

任何线索?

2 个答案:

答案 0 :(得分:1)

我认为这是由于授权问题而发生的,您正在使用无法访问该方法的数据调用方法,并将其重定向到某个地方。当我尝试使用状态代码401(未授权)重定向时,我遇到了类似的问题

答案 1 :(得分:0)

你有没有尝试过,puts response.body在那里看看有什么事情发生?

在哈特尔的导轨教程中有一个模糊的东西帮助我在类似的情况下...我在类似的设计情况下应用他的技巧,老实说,我不完全确定没有戳你的重定向的代码是否来自错误的URI - 但我认为我将此处留给其他有问题的人。在找到自己设计重定向的答案时,我偶然发现了你的帖子(尽管我可能与ID未找到问题有关)。

When I originally wrote this chapter, I couldn’t recall offhand how to escape URLs in Rails, and figuring it out was pure technical sophistication (Box 1.1). What I did was Google “ruby rails escape url”, which led me to find two main possibilities, URI.encode(str) and CGI.escape(str). Trying them both revealed that the latter works. (It turns out there’s a third possibility: the ERB::Util library supplies a url_encode method that has the same effect.)