I'm writing a spec for a RESTFul controller I have using RSpec and I need to log in the user via Devise.
The following code results in the user being signed in properly in the first it
statement, but not the second.
If I change before
and after
from :all
to :each
, both statements fail as if the user is not logged in.
RSpec.describe SomeController, type: :controller do
describe '#index' do
before :all do
@account = create(:account)
@user = @account.users.first
sign_in(@user)
end
after :all do
sign_out(@user)
end
it 'should load all objects of a @user' do
get :index
body = JSON.parse(response.body)
expect(body['data'].length).to eq(4)
expect(response).to have_http_status(:ok)
end
it 'should load all objects of a @user' do
get :index
body = JSON.parse(response.body)
expect(body['data'].length).to eq(4)
expect(response).to have_http_status(:ok)
end
end
end
I found few answers to this issue, one being using Cookie Store in tests ENV (which didn't fix it) and also this one RSpec with devise - only the first test is logged in which also didn't help me with the issue.
RSpec 3.7 Rails 5.2 Devise 4.4
Help will be highly appreciated! thanks
config.before(:suite)
– Udibefore(:each)
in this case? Otherwise Database cleaner will clear the login. – bo-ozbefore :each
doesn't work as well. I've disabled DB Cleaner and still - same results. – Udi