Devise & RSpec - 用户仅在第一次测试中登录
Devise & RSpec - the user is signed in only in the first test
我正在为我使用 RSpec 的 RESTFul 控制器编写规范,我需要通过 Devise 登录用户。
以下代码导致用户在第一个 it
语句中正确登录,但在第二个语句中没有。
如果我将 before
和 after
从 :all
更改为 :each
,这两个语句都会失败,就好像用户未登录一样。
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
我发现这个问题的答案很少,一个是在测试 ENV 中使用 Cookie Store(没有解决它),另一个 也没有帮助我解决这个问题。
RSpec 3.7
Rails 5.2
设计 4.4
非常感谢您的帮助!谢谢
如果您正在使用 ActAsTenant,请不要忘记在 before(:each) 的开头放置 ActAsTenant.current_tenant = nil。这给我造成了与您描述的相同的问题
我正在为我使用 RSpec 的 RESTFul 控制器编写规范,我需要通过 Devise 登录用户。
以下代码导致用户在第一个 it
语句中正确登录,但在第二个语句中没有。
如果我将 before
和 after
从 :all
更改为 :each
,这两个语句都会失败,就好像用户未登录一样。
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
我发现这个问题的答案很少,一个是在测试 ENV 中使用 Cookie Store(没有解决它),另一个
RSpec 3.7 Rails 5.2 设计 4.4
非常感谢您的帮助!谢谢
如果您正在使用 ActAsTenant,请不要忘记在 before(:each) 的开头放置 ActAsTenant.current_tenant = nil。这给我造成了与您描述的相同的问题