0

我有这个功能测试(商店模型有唯一性验证):

feature "Index" do
  before do
    3.times { Store.make! }
  end
  scenario "User visit index" do
    visit stores_path
     within("table.table") do
       Store.all.each do |store|
         expect(page).to have_content(store.name)
       end
     end
   end
end

当我运行测试时,随机失败:

Failure/Error: 3.times { Store.make! }
 ActiveRecord::RecordInvalid:
   Validation failed: name has already been taken

我的蓝图是(按照机械师文档生成唯一记录):

Store.blueprint do
  name { "store #{sn}" }
end

我使用 DatabaseCleaner gem,然后是我的配置 rails_spec:

ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'shoulda/matchers'
require 'capybara/rails'
require 'capybara/rspec'
require 'pundit/rspec'
require 'database_cleaner'

Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|

  config.mock_with :rspec
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = false


  config.before(:each) do |example|
    DatabaseCleaner.strategy = if example.metadata[:js]
      :truncation
    else
      :transaction
    end
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end

  DatabaseCleaner.logger = Rails.logger

  config.infer_spec_type_from_file_location!

错误让我发疯

4

1 回答 1

0

Try to replace the following in your spec/spec_helper.rb:

config.before :suite do |example|
  DatabaseCleaner.strategy = example.metadata[:js] ? :truncation : :transaction
end

config.before do
  DatabaseCleaner.clean
end
于 2014-09-29T02:19:44.130 回答