Rspec - Devise login_as does not work on the first test

六月ゝ 毕业季﹏ 提交于 2021-02-08 09:54:38

问题


I have a controller spec that needs a super admin to be logged in to work

I followed this guide to and wrote the (paraphrased) following in my tests

before(:each) do
  super_admin = FactoryGirl.create(:super_admin)
  login_as super_admin, scope: :super_admin
end

it "does whatever" do
  get :whatever

  expect(whatever).to eq(whatever)
end

it "does something else" do
  get :something_else

  expect(something_else).to (something_else)
end

My problem is that the login is only working for tests past the first, debugging revealed that it was failing authentication, if I use before(:each) to login, and is failing every test except the first when i use before(:all)

I found login_as with rspec fails first test where the asker appears to be having a similar problem, but the only answer doesn't help me


回答1:


So, it looks like login_as doesn't play well with controller specs (I'm assuming), when I switched login_as super_admin, scope: :super_admin with sign_in super_admin everything started working



来源:https://stackoverflow.com/questions/39877687/rspec-devise-login-as-does-not-work-on-the-first-test

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