问题
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
In my rails_helper
I'm including include Warden::Test::Helpers
.
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?
Thanks
回答1:
Use before(:each)
, not before(:context)
:
before(:each) do # <-- !!!!
# ..
login_as(@user, :scope => :user)
end
after(:each) do
Warden.test_reset!
end
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.
来源:https://stackoverflow.com/questions/48720669/rspec-with-devise-only-the-first-test-is-logged-in