0
votes

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

1
do you use databasecleaner? if so evertything is wiped out after each testapneadiving
@apneadiving I am, it's done in config.before(:suite)Udi
Shouldn't you use before(:each) in this case? Otherwise Database cleaner will clear the login.bo-oz
@bo-oz before :each doesn't work as well. I've disabled DB Cleaner and still - same results.Udi

1 Answers

0
votes

If you are using ActAsTenant, do not forget to put ActAsTenant.current_tenant = nil, at the beginning of the before(:each). That caused to me the same problem that you are discribing