RSpec/Capybara 请求规范 - 无法让 Devise 发布新用户会话

发布于 2025-01-02 12:04:37 字数 970 浏览 2 评论 0 原文

我正在尝试通过创建一个发布新用户会话的帮助程序来启动一个新用户会话。这是我所拥有的

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独患者 2025-01-09 12:04:37

我通过包括 Warden::Test::Helpers (该设计在幕后使用)来实现此目的,

require 'spec_helper'
include Warden::Test::Helpers

describe "...Whatever..." do

  before(:each) do
    @user = Factory.create(:user)
    login_as @user, :scope => :user
  end
  ...

从这里找到了解决方案: 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)

require 'spec_helper'
include Warden::Test::Helpers

describe "...Whatever..." do

  before(:each) do
    @user = Factory.create(:user)
    login_as @user, :scope => :user
  end
  ...

found the solution from here: http://blog.joshmcarthur.com/2011/06/11/integration-tests-with-devise-and-rspec/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文