RSpec 控制台日志 returns 302,但测试日志显示 401
RSpec console log returns 302, but test log shows 401
我编写了一个简单的测试来检查用户是否无法 post 进入带有 before_action :authenticate_user!
的控制器。测试内容如下:
context 'when the user is not logged in' do
it 'returns 401' do
sign_out user
post wikis_path(params: wiki)
expect(response).to have_http_status(401)
end
end
控制台输出为:
Failure/Error: expect(response).to have_http_status(401)
expected the response to have status code 401 but it was 302
但是当我检查测试日志时它确实显示 401:
Processing by WikisController#create as HTML
Parameters: {"wiki"=>{"content"=>"Some content"}}
Completed 401 Unauthorized in 4ms (ActiveRecord: 0.0ms)
此外,我可以打开浏览器,删除 auth cookie 并验证它确实以 401 响应。
起初,我认为重定向是 Devise 行为,因为它说它在 their comments, however neither the log or the behavior do this. There is a similar (old) question 用户未使用 Devise 的情况下,但得到相同的结果。我承认,我无法理解所有 Devise 代码,但我能够查找并跟踪 Rails 代码中的 authenticate_or_request_with_http_token
一直到最后,看到它应该有(在他的情况下)返回 401.
控制台中失败的 test/output 与 test.log
中记录的结果不一致的原因是什么?
实际上,第一个状态代码是 302(重定向),然后是 401。我不知道为什么 test.log
不显示 302。下面是重定向案例的脚本(参考:https://relishapp.com/rspec/rspec-rails/docs/matchers/have-http-status-matcher)
context 'when the user is not logged in' do
it 'returns 401' do
sign_out user
post wikis_path(params: wiki)
expect(response).to have_http_status(302)
follow_redirect!
expect(response).to have_http_status(401)
end
end
我编写了一个简单的测试来检查用户是否无法 post 进入带有 before_action :authenticate_user!
的控制器。测试内容如下:
context 'when the user is not logged in' do
it 'returns 401' do
sign_out user
post wikis_path(params: wiki)
expect(response).to have_http_status(401)
end
end
控制台输出为:
Failure/Error: expect(response).to have_http_status(401)
expected the response to have status code 401 but it was 302
但是当我检查测试日志时它确实显示 401:
Processing by WikisController#create as HTML
Parameters: {"wiki"=>{"content"=>"Some content"}}
Completed 401 Unauthorized in 4ms (ActiveRecord: 0.0ms)
此外,我可以打开浏览器,删除 auth cookie 并验证它确实以 401 响应。
起初,我认为重定向是 Devise 行为,因为它说它在 their comments, however neither the log or the behavior do this. There is a similar (old) question 用户未使用 Devise 的情况下,但得到相同的结果。我承认,我无法理解所有 Devise 代码,但我能够查找并跟踪 Rails 代码中的 authenticate_or_request_with_http_token
一直到最后,看到它应该有(在他的情况下)返回 401.
控制台中失败的 test/output 与 test.log
中记录的结果不一致的原因是什么?
实际上,第一个状态代码是 302(重定向),然后是 401。我不知道为什么 test.log
不显示 302。下面是重定向案例的脚本(参考:https://relishapp.com/rspec/rspec-rails/docs/matchers/have-http-status-matcher)
context 'when the user is not logged in' do
it 'returns 401' do
sign_out user
post wikis_path(params: wiki)
expect(response).to have_http_status(302)
follow_redirect!
expect(response).to have_http_status(401)
end
end