带设计的RSpec-仅第一个测试已登录 [英] RSpec with devise - only the first test is logged in
问题描述
我正在尝试为必须使用devise登录的页面编写rspec功能测试。
I'm trying to write a rspec feature test for a page where the user must be logged in with devise.
RSpec.feature "User sees items" do
before(:context) do
..
login_as(@user, :scope => :user)
end
after(:each) do
Warden.test_reset!
end
it "sees 3 items" do
create_items 1, 2
visit root_path
expect(page).to have_css("#total", text: "3")
end
it "sees 7 items" do
create_items 3, 4
visit root_path
expect(page).to have_css("#total", text: "7")
end
end
在我的 rails_helper
中,我包括 include Warden :: Test :: Helpers
。
但是只有我的第一个值3的用户登录。
第二个测试存在设计错误您需要登录或注册后才能继续。
在测试的
But only my first test with value 3 the user is logged in.
The second one there is an devise error You need to sign in or sign up before continuing.
in the page.body
of the test.
如何保持登录状态?还是我每次必须重新登录?
How can I stay logged? Or do I have to log in each time again?
谢谢
推荐答案
使用 before(:each)
,而不是 before(:context)
:
before(:each) do # <-- !!!!
# ..
login_as(@user, :scope => :user)
end
after(:each) do
Warden.test_reset!
end
您的问题是 before(:context)
仅运行一次 -即在第一次测试运行之前,而 after(:each)
每次运行测试。
Your problem is that the before(:context)
is only running once - i.e. before the first test runs, whereas the after(:each)
is running for each test.
因此,在第一次测试运行之后,您已经退出并且没有重新登录。
Therefore after the first test runs, you've signed out and are not signing back in.
这篇关于带设计的RSpec-仅第一个测试已登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!