gpt4 book ai didi

ruby-on-rails - 使用 Devise 测试成功登录

转载 作者:行者123 更新时间:2023-12-04 06:20:03 25 4
gpt4 key购买 nike

我正在尝试阅读 Michael Hartl 的 RoR 教程,但这是我第三次阅读该教程,现在我正在尝试实现 Devise 而不是教程中的家庭滚动身份验证。可以想象,这有点痛苦,主要是因为 RoR 教程中的测试驱动开发与 Devise 没有很好地混合。我目前的挂断是我似乎无法成功测试成功登录。我的网站在本地主机上,所以我知道登录有效,但我的测试由于某种原因仍然失败。这是集成测试片段

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

我得到的错误是
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

顶部的登录栏从 Sign in 切换而来至 Account ,所以看起来测试没有让用户在设置块中成功登录。另一个可能不相关的问题是,在我的另一个测试中,我尝试了与 post user_session_path... 完全相同的登录方法。然后检查 current_user assert_not current_user.nil? 不为空,但我收到错误
NameError: undefined local variable or method 'current_user' for #<UsersLoginTest:0x00557f81155648>

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

编辑:我试过了
post_via_redirect user_session_path, 'user[email]' => @user.email, 'user[password]' => @user.password

而不是之前测试登录的方法,但我收到以下错误
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>'

编辑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

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

最佳答案

我有一个类似的问题,可能会帮助您找到答案。我相信我的装置不正确,因此 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

并且 log/test.log 显示正确的电子邮件正在通过,但是:
Completed 401 Unauthorized in 1ms (ActiveRecord: 0.0ms)

编辑:

在进一步调查中,我认为应该使用 Devise helper 来完成 Devise 集成测试。 .我无法让它工作,但我的理解来自 Google group是文档和 gem 可能暂时不同步。

编辑以下有关在单元和 Controller 中进行测试的进一步评论:

我在单元测试和 Controller 中使用 sign_in @user 并且工作正常。如下所示的 Controller 测试运行良好:
@user = users(:one)
@other_user = users(:two)
sign_in @user
get :edit, id: @other_user
assert_redirected_to root_url

关于ruby-on-rails - 使用 Devise 测试成功登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37956899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com