我正在尝试测试我在rails中的Devise登录,但无论我做什么,我都会收到401未经授权的错误。我想我已经把我的测试设置错误了。我不认为这是一个语法错误。
这是我的灯具的样子。
one:
firstname: John
lastname: Smith
email: example@email.com
encrypted_password: <%= Devise::Encryptor.digest(Traveler, 'password') %>
confirmed_at: 2016-01-02 08:31:23
confirmation_sent_at: 2016-01-02 08:30:59
这就是我的控制器(集成)测试的样子。
require 'test_helper'
class TourControllerTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
def setup
@traveler = travelers(:one)
end
test "successful login of confirmed user" do
get traveler_session_path
assert_equal 200, status
post traveler_session_path 'traveler[email]' => @traveler.email, 'traveler[password]' => @traveler.encrypted_password
follow_redirect!
assert_equal 200, status
end
end
我的test.log看起来像这样
-----------------------------------------------------------
TourControllerTest: test_successful_login_of_confirmed_user
-----------------------------------------------------------
[1m[36mTraveler Load (0.1ms)[0m [1m[34mSELECT "travelers".* FROM "travelers" WHERE "travelers"."id" = ? LIMIT ?[0m [["id", 980190962], ["LIMIT", 1]]
Started GET "/login" for 127.0.0.1 at 2018-03-31 23:43:28 -0400
Processing by Devise::SessionsController#new as HTML
Rendering devise/sessions/new.html.erb within layouts/application
Rendered devise/sessions/new.html.erb within layouts/application (7.9ms)
Rendered shared/_navbar.html.erb (3.6ms)
Rendered shared/_message.html.erb (0.9ms)
Completed 200 OK in 559ms (Views: 557.2ms | ActiveRecord: 0.0ms)
Started POST "/login?traveler%5Bemail%5D=example%40email.com&traveler%5Bpassword%5D=[FILTERED]" for 127.0.0.1 at 2018-03-31 23:43:29 -0400
Processing by Devise::SessionsController#create as HTML
Parameters: {"traveler"=>{"email"=>"example@email.com", "password"=>"[FILTERED]"}}
[1m[36mTraveler Load (0.4ms)[0m [1m[34mSELECT "travelers".* FROM "travelers" WHERE "travelers"."email" = ? ORDER BY "travelers"."id" ASC LIMIT ?[0m [["email", "example@email.com"], ["LIMIT", 1]]
Completed 401 Unauthorized in 6ms (ActiveRecord: 0.4ms)
Processing by Devise::SessionsController#new as HTML
Parameters: {"traveler"=>{"email"=>"example@email.com", "password"=>"[FILTERED]"}}
Rendering devise/sessions/new.html.erb within layouts/application
Rendered devise/sessions/new.html.erb within layouts/application (1.1ms)
Rendered shared/_navbar.html.erb (1.0ms)
Rendered shared/_message.html.erb (0.1ms)
Completed 200 OK in 7ms (Views: 5.0ms | ActiveRecord: 0.0ms)
[1m[35m (0.1ms)[0m [1m[31mrollback transaction[0m
答案 0 :(得分:0)
尝试替换此行
post traveler_session_path 'traveler[email]' => @traveler.email, 'traveler[password]' => @traveler.encrypted_password
到
post traveler_session_path 'traveler[email]' => @traveler.email, 'traveler[password]' => 'password'
根据encrypted_password: <%= Devise::Encryptor.digest(Traveler, 'password') %>
我认为测试需要通过电子邮件登录:'example@email.com'/密码:'密码'