运行 Capybara Specs 只能使用一次;之后,工厂出现问题
Running Capybara Specs only works one time; after, problems with factories
我目前在运行水豚规格方面遇到问题。我第一次 运行 他们按预期工作并通过。每次之后,我都会收到以下错误:
Validation failed: Name has already been taken
这是我的 sign_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
来管理测试之间的数据库清理。将 database_cleaner gem 添加到您的项目中,然后使用 database_cleaner 存储库中为 Capybara 显示的配置 - https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example.
您还需要确保在 spec_helper/rails_helper
中注释掉任何其他提及 config.use_transactional_fixtures
的内容
我目前在运行水豚规格方面遇到问题。我第一次 运行 他们按预期工作并通过。每次之后,我都会收到以下错误:
Validation failed: Name has already been taken
这是我的 sign_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
来管理测试之间的数据库清理。将 database_cleaner gem 添加到您的项目中,然后使用 database_cleaner 存储库中为 Capybara 显示的配置 - https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example.
您还需要确保在 spec_helper/rails_helper
中注释掉任何其他提及config.use_transactional_fixtures
的内容