使用 Rspec 和 Rspec 在 Rails 3 中测试会话水豚
我正在尝试使用 rspec、factory_girl & 编写集成测试。水豚。我还安装了黄瓜,但我没有使用它(据我所知)。
我基本上想用我的用户预填充数据库,然后转到我的主页并尝试登录。它应该重定向到 user_path(@user)。
但是,会话似乎没有在我的 /rspec/requests/ 集成测试中保留。
我的规范:/rspec/requests/users_spec.rb
require 'spec_helper'
describe "User flow" do
before(:each) do
@user = Factory(:user)
end
it "should login user" do
visit("/index")
fill_in :email, :with => @user.email
fill_in :password, :with => @user.password
click_button "Login"
assert current_path == user_path(@user)
end
end
返回:
Failures:
1) User flow should login user
Failure/Error: assert current_path == user_path(@user)
<false> is not true.
# (eval):2:in `send'
# (eval):2:in `assert'
# ./spec/requests/users_spec.rb:16
相反,它会重定向到我的 please_login_path - 如果由于任何原因登录失败(或者未设置 session[:user_id]),就会发生这种情况。
如果我尝试放置 session.inspect,它会作为 nil 对象失败。
如果我尝试在控制器测试(/rspec/controllers/sessions_spec.rb)中执行此操作,我可以毫无问题地访问会话,并且可以调用 session[:user_id]
I'm trying to write integration tests with rspec, factory_girl & capybara. I also have cucumber installed, but I'm not using it (to my knowledge).
I basically want to prepopulate the db with my user, then go to my home page and try to log in. It should redirect to user_path(@user).
However, sessions don't seem to be persisted in my /rspec/requests/ integration tests.
My spec: /rspec/requests/users_spec.rb
require 'spec_helper'
describe "User flow" do
before(:each) do
@user = Factory(:user)
end
it "should login user" do
visit("/index")
fill_in :email, :with => @user.email
fill_in :password, :with => @user.password
click_button "Login"
assert current_path == user_path(@user)
end
end
Returns:
Failures:
1) User flow should login user
Failure/Error: assert current_path == user_path(@user)
<false> is not true.
# (eval):2:in `send'
# (eval):2:in `assert'
# ./spec/requests/users_spec.rb:16
Instead, it redirects to my please_login_path - which should happen if the login fails for any reason (or if session[:user_id] is not set).
If I try to put session.inspect, it fails as a nil object.
If I try to do this in the controller tests (/rspec/controllers/sessions_spec.rb), I can access the session with no problem, and I can call session[:user_id]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用 Devise,则需要包含 Warden::Test::Helpers (紧接在 spec_helper 的 require 之后是一个好地方),如下所示 典狱长维基。
对会话的调用返回 nil,因为水豚在作为集成测试运行时不提供对它的访问。
If you are using Devise, you'll need to include Warden::Test::Helpers (right after the require of spec_helper is a good place) as outlined in the warden wiki.
The call to session is returning nil because capybara doesn't provide access to it when running as an integration test.
我也有同样的问题,虽然填写表格可能是某些人的选择,但我不得不推出自己的身份验证 ruby,因为我使用的是第三方身份验证系统(确切地说是 Janrain)....在我的测试中我结束了使用这样的东西:
这是我在spec/support/test_helpers_and_stuff.rb中的内容
然后在我的请求规范中,使用水豚和硒,我做了以下操作:
顺便说一句,在尝试了建议的解决方案后,我提出了解决方案< a href="https://stackoverflow.com/questions/5865555/how-to-do-integration-testing-with-rspec-and-devise-cancan">这篇文章和这篇文章,它们都不适合我。 (但他们确实启发了我的解决方案)
祝你好运!
I have the same problems and although filling out a form might be an option for some, I had to roll my own authentication ruby because I was using a third party auth system (Janrain to be exact).... in my tests I ended up using something like this:
Here is what I have in my spec/support/test_helpers_and_stuff.rb
Then in my request spec, with capybara and selenium, I did the following:
BTW, I came up with solutions after trying the proposed solutions to this post and this post and neither of them worked for me. (but they certainly inspired my solution)
Good luck!
您可能已经离开了这个话题,但我只是在同一个问题上苦苦挣扎。事实证明这是一个语法问题。我使用的是
:email
和:password
符号,我应该使用字符串来代替("email"
和"password “
)。换句话说,尝试将其更改
为:
You've probably moved on from this, but I was just struggling with the same question. Turns out it was a matter of syntax. I was using symbols for
:email
and:password
and I should've been using strings instead ("email"
and"password"
).In other words, try changing this:
to this: