【发布时间】:2017-01-10 00:45:36
【问题描述】:
我遇到了非常奇怪的测试行为,登录状态处理不一致。
规范让用户登录,访问(嵌套或非嵌套)索引页面,并检查显示的内容是否正确。记录是异步获取的,但我认为这不会产生影响。
当每个规范单独运行时,它们都通过了。当所有规范一起运行时,它们会失败,因为缺少预期的内容。使用save_and_open_page 表明这是因为正在呈现登录页面,而不是预期的索引页面。
为什么当所有规范一起运行时,rspec 认为用户没有登录,但每个规范都单独通过?
测试看起来像这样
let(:user) {create :user}
let(:team) {create :team}
let(:country) {create :country}
before :each do
login_as( user, scope: :user )
end
describe 'unnested' do
it 'should have the expected content', :js do
visit users_path
is_expected.to have_content "some content on the page"
end
end
describe 'nested by team' do
it 'should have the expected content', :js do
visit team_users_path(team)
is_expected.to have_content "some content on the page"
end
end
describe 'nested by nationality' do
it 'should have the expected content', :js do
visit country_users_path(country)
is_expected.to have_content "some content on the page"
end
end
规范都需要javascript(我不知道这是否重要)。
身份验证由Devise处理,我的rails_helper.rb包括
config.append_after(:each) do
DatabaseCleaner.clean
Warden.test_reset!
end
为什么当所有规范一起运行时,rspec 认为用户没有登录,但每个规范都单独通过?
【问题讨论】:
标签: ruby-on-rails rspec devise