使用Devise测试成功登录

mjs*_*rtz 3 ruby-on-rails devise

我正在尝试通过Michael Hartl的RoR教程,但这是我第三次通过这个东西,现在我正在尝试在教程中实现Devise而不是home roll认证.可以想象,这有点痛苦,主要是由于RoR教程中的测试驱动开发与Devise不能很好地混合.我目前挂断的是,我似乎无法成功测试成功登录.我有关于localhost的网站,所以我知道登录有效,但我的测试仍然因某些原因而失败.这是集成测试代码段

def setup
  @user = users(:michael)
end

...

test "login with valid information" do
  get user_session_path
  assert_equal 200, status
  post user_session_path, 'user[email]' => @user.email, 'user[password]' => "password" 
  follow_redirect!
  assert_equal 200, status
  assert_select "a[href=?]", user_session_path, count: 0 # THIS LINE FAILS
  assert_select "a[href=?]", destroy_user_session_path
  assert_select "a[href=?]", user_path(@user)
end
Run Code Online (Sandbox Code Playgroud)

我得到的错误是

FAIL["test_login_with_valid_information", UsersLoginTest, 2016-06-19 11:06:18 -0400]
test_login_with_valid_information#UsersLoginTest (1466348778.61s)
      Expected exactly 0 elements matching "a[href="/users/sign_in"]", found 1..
      Expected: 0
        Actual: 1
Run Code Online (Sandbox Code Playgroud)

顶部的登录栏切换Sign inAccount,所以看起来测试没有让设置块中的用户成功登录.另一个可能不相关的问题是,在我的另一个测试中,我尝试相同的确切登录方法,post user_session_path...然后检查是否current_user为空assert_not current_user.nil?,但我得到错误

NameError: undefined local variable or method 'current_user' for #<UsersLoginTest:0x00557f81155648>
Run Code Online (Sandbox Code Playgroud)

我环顾四周,确保devise_for :users在routes.rb文件中有正确的行.我很确定它无法访问,current_user因为我不知道我的集成测试能够访问Devise中的资源.那是我的两个问题!任何帮助将不胜感激.

编辑:我试过了

post_via_redirect user_session_path, 'user[email]' => @user.email, 'user[password]' => @user.password
Run Code Online (Sandbox Code Playgroud)

而不是以前测试登录的方法,但我得到以下错误

ERROR["test_login_with_valid_information", UsersLoginTest, 2016-06-28 20:40:59 -0400]
 test_login_with_valid_information#UsersLoginTest (1467160859.16s)
RuntimeError:         RuntimeError: not a redirect! 200 OK
            test/integration/users_login_test.rb:36:in `block in <class:UsersLoginTest>'
        test/integration/users_login_test.rb:36:in `block in <class:UsersLoginTest>'

ERROR["test_login_with_valid_information_followed_by_logout", UsersLoginTest, 2016-06-28 20:40:59 -0400]
 test_login_with_valid_information_followed_by_logout#UsersLoginTest (1467160859.18s)
NameError:         NameError: undefined local variable or method `current_user' for #<UsersLoginTest:0x0055d0b9e0df50>
            test/integration/users_login_test.rb:49:in `block in <class:UsersLoginTest>'
        test/integration/users_login_test.rb:49:in `block in <class:UsersLoginTest>'
Run Code Online (Sandbox Code Playgroud)

编辑2:

我使用了Jemima建议的夹具并重新进行了测试.在检查测试日志后,我找到了

-------------------------------------------------
UsersLoginTest: test_login_with_valid_information
-------------------------------------------------
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 762146111]]
Started GET "/users/sign_in" for 127.0.0.1 at 2016-07-01 18:46:41 -0400
Processing by Devise::SessionsController#new as HTML
  Rendered devise/shared/_links.html.erb (0.4ms)
  Rendered devise/sessions/new.html.erb within layouts/application (1.6ms)
  Rendered layouts/_shim.html.erb (0.1ms)
  Rendered layouts/_header.html.erb (0.3ms)
  Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 4ms (Views: 3.4ms | ActiveRecord: 0.0ms)
Started POST "/users/sign_in" for 127.0.0.1 at 2016-07-01 18:46:41 -0400
Processing by Devise::SessionsController#create as HTML
  Parameters: {"user"=>{"email"=>"michael@example.com", "password"=>"[FILTERED]"}}
Completed 401 Unauthorized in 0ms (ActiveRecord: 0.0ms)
Processing by Devise::SessionsController#new as HTML
  Parameters: {"user"=>{"email"=>"michael@example.com", "password"=>"[FILTERED]"}}
  Rendered devise/shared/_links.html.erb (0.5ms)
  Rendered devise/sessions/new.html.erb within layouts/application (2.1ms)
  Rendered layouts/_shim.html.erb (0.1ms)
  Rendered layouts/_header.html.erb (0.3ms)
  Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 6ms (Views: 5.3ms | ActiveRecord: 0.0ms)
   (0.1ms)  rollback transaction
   (0.1ms)  begin transaction
Run Code Online (Sandbox Code Playgroud)

所以它看起来像是未经授权的?此外,我试图include Devise::Test::IntegrationHelpers在我的用户登录测试的顶部,但它只是抛出错误NameError: uninitialized constant Devise::Test.

Jem*_*ima 6

我有一个类似的问题可能会帮助你找到答案.我相信我的夹具不正确,因此Devise没有进行身份验证.

我的用户夹具,以防它帮助您:

user_no_club:
  email: noclub@example.com
  encrypted_password: <%= Devise::Encryptor.digest(User, 'password') %>
  confirmed_at: 2016-01-02 08:31:23
  confirmation_sent_at: 2016-01-02 08:30:59
Run Code Online (Sandbox Code Playgroud)

并且log/test.log显示正确的电子邮件已通过,但随后:

Completed 401 Unauthorized in 1ms (ActiveRecord: 0.0ms)
Run Code Online (Sandbox Code Playgroud)

编辑:

在进一步调查中,我认为应该使用Devise帮助程序完成Devise集成测试.我无法让它发挥作用,但我对Google小组的理解是,文档和gem可能暂时不同步.

编辑以下关于单位和控制器测试的进一步评论:

我在单元测试和控制器中使用sign_in @user,并且工作正常.以下控制器测试运行良好:

@user = users(:one)
@other_user = users(:two)
sign_in @user
get :edit, id: @other_user
assert_redirected_to root_url
Run Code Online (Sandbox Code Playgroud)