简体   繁体   English

使用Devise登录后测试重定向

[英]Testing redirect after login with Devise

I've followed the recommendation from the Devise github pages for this: 我遵循了Devise github页面上的建议:

http://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in http://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in

Now this works great, but how would you test that you have this behavior now? 现在这很好用,但是您将如何测试您现在是否有这种行为?

Well there are two ways of testing it one in the unit level by writing tests in the controllers that inherit the application controller. 那么,通过在继承应用程序控制器的控制器中编写测试,可以在单元级别进行两种测试。 The code will look something like 该代码看起来像

it "should redirect to page_x after logged in" do
  sign_in :user_role, @user 
  set_devise_mapping(:user_role) 
  get :new 
  response.should redirect_to(user_roles_dashboard_path) 
end

For cucumber you should probably write a step to do the login and assert if u are on the expected after sign_in page. 对于黄瓜,您可能应该编写一个步骤来登录并断言u是否在登录后的预期页面上。

Hm... I think you should write own integration tests to check the behavior. 嗯...我想您应该编写自己的集成测试来检查行为。 No need of unit tests or functional tests if you did not mess with the Devise code. 如果您不对Devise代码感到困惑,则无需进行单元测试或功能测试。

If you are just using mini test, it would be something like this: 如果您只是使用迷你测试,则可能是这样的:

require 'test_helper'

class SessionsControllerTest < ActionDispatch::IntegrationTest
  include Devise::Test::IntegrationHelpers

  test "admins should be redirected to comments_url" do
    sign_in(users(:one))
    post user_session_url
    assert_redirected_to comments_url
  end

  test "No admins should be redirected to root_path" do
    sign_in(users(:two))
    post user_session_url
    assert_redirected_to root_url
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM