[英]Devise and Rspec - error in controller test when testing redirect for unauthenticated users
I'm trying to write a test that makes sure unauthenticated useres are not able to access a certain page. 我正在尝试编写一个测试,以确保未经身份验证的用户无法访问特定页面。 Here is my spec 这是我的规格
it "redirects to the sign in page for unauthenticated users" do
bob = Fabricate(:user)
sign_out bob
carolina_skiff = Fabricate(:boat_brand)
get :edit, id: carolina_skiff.id
expect(response).to redirect_to new_user_session_path
end
When I run this I get this error. 当我运行它时,我得到这个错误。
Failure/Error: get :edit, id: carolina_skiff.id
ArgumentError:
uncaught throw :warden
It seems like I'm not able to log out a user. 看来我无法注销用户。 I have another spec where I'm signing in a user and that one works fine. 我有另一个要登录用户的规范,该规范可以正常工作。
it "sets the @boat_brand" do
bob = Fabricate(:user)
sign_in bob
carolina_skiff = Fabricate(:boat_brand)
get :edit, id: carolina_skiff.id
expect(assigns(:boat_brand)).to eq(carolina_skiff)
end
Any help would be greatly appreciated. 任何帮助将不胜感激。
I don't know if it will help, but I came across similar problem when trying to do functional tests of devise redirection after log in. These are important points: 我不知道这是否有帮助,但是在登录后尝试进行设计重定向的功能测试时遇到了类似的问题。这些要点:
Devise::SessionsController
(not eg. UsersController) or write test Devise::SessionsController
精确测试Devise::SessionsController
(例如,不能使用UsersController)或编写test Devise::SessionsController
Sample test with redirection can look like this: 带有重定向的样本测试如下所示:
require "test_helper"
describe 'Devise::SessionsControllerTest' do
# If you want different name in describe block write:
# tests Devise::SessionsController
include Devise::TestHelpers
test "should redirect to page_x after logged in" do
@request.env["devise.mapping"] = Devise.mappings[:user]
user = users :user
sign_in user
get :new
assert_redirected_to orders_path
end
end
Where :user
is just regular valid devise user, and orders_path
is expected redirect path after log in. This is not exact log in test but rather test of the redirection from login page for logged in user (to be precise). 其中:user
只是常规的有效devise用户,而orders_path
是登录后的重定向路径。这不是精确的登录测试,而是对登录用户从登录页面进行重定向的测试(准确地说)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.