我已经在我的rails应用程序中设计了身份验证,并且在测试一些路由时,我发现调用before(:all)
与before(:each)
导致了不同的身份验证。
以下测试通过:
require 'rails_helper'
RSpec.describe ClientsController, type: :controller do
describe "user signed in" do
before(:all) do
@client = create(:client)
login_as @client.user, scope: :user
end
it 'expects success in accessing profile when signed in' do
get :profile
expect(response).to have_http_status(:success)
end
end
end
以下测试失败:
require 'rails_helper'
RSpec.describe ClientsController, type: :controller do
describe "user signed in" do
before(:each) do
@client = create(:client)
login_as @client.user, scope: :user
end
it 'expects success in accessing profile when signed in' do
get :profile
expect(response).to have_http_status(:success)
end
end
end
唯一的区别是将before(:each)
更改为before(:all)
。还值得注意的是,如果我尝试在get :profile
之后访问任何其他受保护的页面,它们也会同样失败。
为什么会这样,我该如何解决?