我有一个使用Mongo DB的RoR应用程序。测试的功能在开发/生产中正常工作。测试套件曾经工作过。但是,第一个之后的所有测试现在都失败了。
具体来说,如果使用:js标志进行测试:
it 'has activity log', :js do
visit activity_log_site_settings_site_statistics_path
expect(page).to have_content 'Activity Log'
end
第一次这样的测试工作。但是,使用:js标志的所有后续测试都会失败,因为Mongo无法在数据库中找到记录:
Problem:
Document(s) not found for class User with id(s) 59c145db9757d1ce3500000c.
使用FactoryGirl驱动应用程序来填充数据库。我在测试之间使用DatabaseCleaner(在rails_helper.rb中):
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
我试过了:
config.use_transactional_fixtures = false
和shared_connection修正:https://gist.github.com/josevalim/470808
这是套件中的实际测试。 :js标志是不必要的,我只是添加它们,因为这是一个更简单的测试:
require 'rails_helper'
describe 'SiteStatistics', :vcr do
before do
create_xml_feeds
@site_admin = FactoryGirl.create :confirmed_user, site_admin: 'true'
@organization = FactoryGirl.create :organization
@bulletin = Bulletin.create(organization: @organization, user: @site_admin, title: 'Test Title', publish_date: '2013-5-12')
login_user @site_admin
end
context 'Activity Log' do
it 'has activity log', :js do
visit activity_log_site_settings_site_statistics_path
expect(page).to have_content 'Activity Log'
end
it 'shows created bulletin', :js do
visit activity_log_site_settings_site_statistics_path
expect(page).to have_content 'test user created a new bulletin'
expect(page).to have_content 'Test Organization, Flagstaff, AZ'
end
end
end
ruby 2.1.0p0,Rails 3.2.22.5