简单的预生成测试不起作用。它被更改为更简单,但数据在测试后没有被恢复。所以它第一次运行,传递,然后随着计数的增加每次都失败。
我在Mac上运行,MariaDb安装了innoDB引擎。
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
答案 0 :(得分:0)
您应该使用 DataBaseCleaner gem:
在您的Gemfile中添加以下行:gem 'database_cleaner'
,然后运行bundle install
。
然后,将这些行包含在 Spec.configure 块中:
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
希望这有帮助:)