简单的预先生成的测试不起作用。它已变得更加简单,但测试后数据并未恢复。因此,它第一次运行时通过,然后随着计数的增加而失败。
我在 Mac 上运行,安装了带有 innoDB 引擎的 MariaDb。
rails_helper.rb
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
config.include Devise::TestHelpers, type: :controller
config.include FactoryGirl::Syntax::Methods
end
工厂
FactoryGirl.define do
factory :tag do
icon
word
end
end
FactoryGirl.define do
factory :word do
word 'TestWord'
end
end
FactoryGirl.define do
factory :icon do
icon_name "Cool Icon"
icon_location "cache/1234"
submission_date "2014-12-15 18:38:20"
end
end
规范
require 'rails_helper'
RSpec.describe TagsController, type: :controller do
# let(:tag) { build(:tag) }
let(:valid_session) { {} }
describe "GET #index" do
it "assigns all tags as @tags" do
@tag = FactoryGirl.create(:tag)
get :index, {}, valid_session
expect(Tag.count).to eq([@tag].count)
end
end
end
最佳答案
您应该使用DataBaseCleaner gem:
在您的 Gemfile 中包含此行:gem 'database_cleaner'
,然后运行 bundle install
。
然后,将此行包含在 Spec.configure block 中:
config.before(:suite) do
begin
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with :truncation
DatabaseCleaner.start
FactoryGirl.lint
ensure
DatabaseCleaner.clean
end
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
希望这有帮助:)
关于mysql - FactoryGirl 和 RSpec 不回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32275478/