数据在 before(:each) 中以 RSpec-rails 持久化
Data persisting with RSpec-rails in a before(:each)
我在 rails_helper...
中设置了交易
config.use_transactional_fixtures = true
我的每个块之前都是这样的:
before(:each) do
allow(subject).to receive_messages(
:authenticate => true,
:authorize => true
)
@user_song = FactoryGirl.create(:user_song)
FactoryGirl.create(:user_playlist, user_id: @user_song.user_id,
album_id: @user_song.song.album_id)
allow_any_instance_of(ApplicationController).to receive_messages(
:current_user => @user_song.user
)
测试因验证错误而失败,但是,如果我在创建 FactoryGirl 之前为每个使用过的模型插入一个 Model.destroy_all
,它就可以工作。我很困惑。
为什么? Rspec rails guide 说应该回滚?跟 FactoryGirl 有关系吗?
您需要安装和设置database cleaner gem. Configured and installed properly, it will fix this problem you're having. There are many blog articles to help with the setup and install if the github read me docs seem confusing. Such as this文章
尝试配置数据库清理器。
将此添加到您的 Gemfile
gem 'database_cleaner'
然后更新你的
spec/rails_helper.rb
:
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
config.use_transactional_fixtures = true
用于 Rails 内置夹具清理。它适用于 factory_girl
。你应该禁用它,然后像这样安装一个 db cleaner gem:https://github.com/DatabaseCleaner/database_cleaner
我在 rails_helper...
中设置了交易config.use_transactional_fixtures = true
我的每个块之前都是这样的:
before(:each) do
allow(subject).to receive_messages(
:authenticate => true,
:authorize => true
)
@user_song = FactoryGirl.create(:user_song)
FactoryGirl.create(:user_playlist, user_id: @user_song.user_id,
album_id: @user_song.song.album_id)
allow_any_instance_of(ApplicationController).to receive_messages(
:current_user => @user_song.user
)
测试因验证错误而失败,但是,如果我在创建 FactoryGirl 之前为每个使用过的模型插入一个 Model.destroy_all
,它就可以工作。我很困惑。
为什么? Rspec rails guide 说应该回滚?跟 FactoryGirl 有关系吗?
您需要安装和设置database cleaner gem. Configured and installed properly, it will fix this problem you're having. There are many blog articles to help with the setup and install if the github read me docs seem confusing. Such as this文章
尝试配置数据库清理器。
将此添加到您的 Gemfile
gem 'database_cleaner'
然后更新你的
spec/rails_helper.rb
:
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
config.use_transactional_fixtures = true
用于 Rails 内置夹具清理。它适用于 factory_girl
。你应该禁用它,然后像这样安装一个 db cleaner gem:https://github.com/DatabaseCleaner/database_cleaner