我正在尝试通过创建一个发布新用户会话的帮助程序来启动一个新用户会话。这是我所拥有的
def login(user)
post user_session_path, :login => user.username, :password => user.password
end
对于用户,
user = Factory.create(:user)
我在 RSpec 中进行了测试,将用户引导到需要身份验证的页面。使用帮助程序,我希望创建一个新的用户会话。但是,运行规范时,它告诉我当前页面是登录屏幕。这表明未创建新的用户会话,并且在没有用户会话的情况下访问受限资源时,用户将被重定向到登录屏幕。查看测试日志,是这样的。
另外查看日志,发现 POST 操作未经授权。
Started POST "/login" for 127.0.0.1 at 2012-02-04 13:34:59 -0800
Processing by SessionsController#create as HTML
Parameters: {"login"=>"foo12", "password"=>"[FILTERED]"}
Completed 401 Unauthorized in 1ms
Processing by SessionsController#new as HTML
Parameters: {"login"=>"foo12", "password"=>"[FILTERED]"}
Rendered devise/shared/_links.erb (1.4ms)
Completed 200 OK in 15ms (Views: 12.7ms | ActiveRecord: 0.0ms)
我尝试通过浏览器手动使用登录页面,并且能够很好地创建会话。我还用 Capybara 编写了一个测试,该测试访问登录页面,填写用户凭据,然后提交。这会毫无问题地创建一个新的用户会话。
I'm trying to start a new user session by creating a helper that POSTs a new user session. Here is what I have
def login(user)
post user_session_path, :login => user.username, :password => user.password
end
And for user
user = Factory.create(:user)
I have a test in RSpec that directs the user to a page that requires authentication. Using the helper, I expect a new user session to be created. However, running the spec, it is telling me the current page is the login screen. This tells a new user session isn't being created and the user is being redirected to the login screen when accessing the restricted resource with no user session. Looking at the test logs, this is the case.
Also looking at the logs, it is saying the POST action is unauthorized.
Started POST "/login" for 127.0.0.1 at 2012-02-04 13:34:59 -0800
Processing by SessionsController#create as HTML
Parameters: {"login"=>"foo12", "password"=>"[FILTERED]"}
Completed 401 Unauthorized in 1ms
Processing by SessionsController#new as HTML
Parameters: {"login"=>"foo12", "password"=>"[FILTERED]"}
Rendered devise/shared/_links.erb (1.4ms)
Completed 200 OK in 15ms (Views: 12.7ms | ActiveRecord: 0.0ms)
I tried manually using the login page through a browser and was able to create a session just fine. I've also wrote up a test with Capybara that visits the login page, fills in the user credentials, and submit. This creates a new user session with no problems.
发布评论
评论(1)
我通过包括 Warden::Test::Helpers (该设计在幕后使用)来实现此目的,
从这里找到了解决方案: http://blog.joshmcarthur.com/2011/06/11/integration-tests-with-devise-and-rspec/
I got this to work by including Warden::Test::Helpers (which devise uses behind the scenes)
found the solution from here: http://blog.joshmcarthur.com/2011/06/11/integration-tests-with-devise-and-rspec/