[英]Devise & RSpec - the user is signed in only in the first test
我正在使用RSpec編寫RESTFul控制器的規范,我需要通過Devise登錄用戶。
以下代碼導致用戶在第一個it
語句中正確登錄,但在第二個語句中未正確登錄。
如果我從:all
更改為:each
before
和after
,這兩個語句都會失敗,就像用戶未登錄一樣。
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存儲(未解決該問題),另一個是具有設計目的的RSpec-僅登錄了第一個測試,但也無濟於事。
RSpec 3.7 Rails 5.2設計4.4
幫助將不勝感激! 謝謝
如果您使用的是ActAsTenant,請不要忘記將ActAsTenant.current_tenant = nil放在before(:each)的開頭。 那給我造成了你所描述的同樣的問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.