运行水豚Specs只运行一次;之后,工厂的问题



我目前在运行水豚规格时遇到问题。我第一次运行它们时,它们按预期工作并通过。每次之后,我都会收到以下错误:

Validation failed: Name has already been taken

这是我的签名_in_spec.rb

require 'rails_helper'
RSpec.describe 'Sign in and client creation page' do
let!(:team_all) { FactoryGirl.create(:team_all) }
let!(:development) { FactoryGirl.create(:team_development, parent: team_all) }
let!(:unassigned) { FactoryGirl.create(:team_unassigned, parent: development) }
let!(:product_feed) { FactoryGirl.create(:team_product_feed, parent: development) }
it 'creates a new client' do
user = FactoryGirl.create(:user, team: team_all)
login_as(user, :scope => :user)
visit ('/clients/new')
fill_in 'client_name', with: 'Capybara'
find('#new_client > input.button.bottom').click
expect(page).to have_content('Add Source')
end
end

successful_source_spec.rb

require 'rails_helper'
RSpec.describe 'Successful source' do
let!(:team_all) { FactoryGirl.create(:team_all) }
let!(:development) { FactoryGirl.create(:team_development, parent: team_all) }
let!(:unassigned) { FactoryGirl.create(:team_unassigned, parent: development) }
let!(:product_feed) { FactoryGirl.create(:team_product_feed, parent: development) }
login_user
it 'is created' do 
fill_in 'origin_name', with: 'Feed'
find('#origin_feed_option_attributes_primary').click
fill_in 'origin_source', with: 'example_url'
find('#origin_remove_html').click
find('#new_origin > div > div > div > input:nth-child(3)').click
find('#new_origin > div > div > div > a').click
expect(page).to have_content('Feed')
end
end

source_error_spec.rb

require 'rails_helper'
RSpec.describe 'Source creation' do
let!(:team_all) { FactoryGirl.create(:team_all) }
let!(:development) { FactoryGirl.create(:team_development, parent: team_all) }
let!(:unassigned) { FactoryGirl.create(:team_unassigned, parent: development) }
let!(:product_feed) { FactoryGirl.create(:team_product_feed, parent: development) }
login_user
it 'creates bad source' do
fill_in 'origin_name', with: 'Rules'
find('#origin_feed_option_attributes_primary').click
fill_in 'origin_source', with: 'example_url'
find('#origin_remove_html').click
find('#new_origin > div > div > div > input:nth-child(3)').click
expect(page).to have_selector('body > div.off-canvas-wrap.full-width > div > section > section > div > div.flash-wrapper-wrapper > div > div > div')
end
end

相关工厂

factory :user do
sequence(:email) { |n| "user#{n}@.com" }
password 'p@ssw0rd'
end
factory :team, class: Teams::ProductFeed do
sequence(:name) { |n| "Team #{n}" }
end
factory :team_all, class: Teams::All do
name 'All'
end
factory :team_development, class: Teams::Development do
name 'Development'
end
factory :team_unassigned, class: Teams::Unassigned do
name 'Unassigned'
end
factory :team_product_feed, class: Teams::ProductFeed do
name 'Product Feed'
end

我能做些什么来防止未来工厂出现问题?如果需要rails_helper和spec_helper的一部分,请告诉我。

由于您使用的是Rails<5.1在使用除rack_test之外的任何驱动程序对Capybara进行测试时,不能使用事务性测试。因此,您需要使用database_cleaner来管理测试之间DB的清理。将database_cleaner gem添加到您的项目中,然后使用database_cleaer repo上显示的Capybara配置-https://github.com/DatabaseCleaner/database_cleaner#rspec-以水豚为例。

您还需要确保在spec_helper/rails_helper 中注释掉任何其他提到的config.use_transactional_fixtures

相关内容

最新更新