首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rspec 3,capybara和machinist 2的问题

rspec 3,capybara和machinist 2的问题
EN

Stack Overflow用户
提问于 2014-09-07 01:03:18
回答 1查看 133关注 0票数 0

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

代码语言:javascript
复制
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

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

代码语言:javascript
复制
Failure/Error: 3.times { Store.make! }
 ActiveRecord::RecordInvalid:
   Validation failed: name has already been taken

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

代码语言:javascript
复制
Store.blueprint do
  name { "store #{sn}" }
end

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

代码语言:javascript
复制
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!

这些错误让我抓狂

EN

回答 1

Stack Overflow用户

发布于 2014-09-29 10:19:44

尝试在您的spec/spec_helper.rb中替换以下内容

代码语言:javascript
复制
config.before :suite do |example|
  DatabaseCleaner.strategy = example.metadata[:js] ? :truncation : :transaction
end

config.before do
  DatabaseCleaner.clean
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25702644

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档