运行Capybara Specs只能运行一次;之后,工厂出现问题

时间:2018-07-09 19:41:42

标签: selenium rspec capybara factory-bot

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

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的一部分,请告诉我。

1 个答案:

答案 0 :(得分:1)

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

您还需要确保在spec_helper / rails_helper

中注释掉对config.use_transactional_fixtures的其他提及。