RSpec with devise - only the first test is logged in

本小妞迷上赌 提交于 2019-12-25 00:56:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!