Ruby on rails FactoryGirl错误:“;电子邮件已被接收。”;

Ruby on rails FactoryGirl错误:“;电子邮件已被接收。”;,ruby-on-rails,ruby,rspec,factory-bot,Ruby On Rails,Ruby,Rspec,Factory Bot,当使用下面的工厂时,我得到一个错误(如下所示)。它通过运行rake db:test:prepare临时修复,但是,这只针对一次测试运行修复它。我能做些什么来获得更持久的修复?我是否应该使用数据库\u cleaner?如果是这样的话,有人能提供一些关于设置它的见解吗(我使用的是RSpec、Capybara、rails4、Ruby 2)?谢谢 我正在使用以下工厂: FactoryGirl.define do factory :user do sequence(:username)

当使用下面的工厂时,我得到一个错误(如下所示)。它通过运行
rake db:test:prepare
临时修复,但是,这只针对一次测试运行修复它。我能做些什么来获得更持久的修复?我是否应该使用
数据库\u cleaner
?如果是这样的话,有人能提供一些关于设置它的见解吗(我使用的是RSpec、Capybara、rails4、Ruby 2)?谢谢

我正在使用以下工厂:

FactoryGirl.define do 
  factory :user do
    sequence(:username)   { |n| "Person #{n}" }
    sequence(:email)      { |n| "person_#{n}@example.com" }
    password "foobar"
    password_confirmation "foobar"

    factory :admin do
      admin true
    end
  end

  factory :key do
    sequence(:name)  { |n| "Key #{n}" }
    sequence(:description)  { |n| "This is key #{n}" }
    root_note "C"
    accidental "Natural"
    third "Major"
    user
  end

  factory :chord do
    root_note "C"
    accidental "Natural"
    third "Major"
    fifth ""
    extension ""
    sequence(:name) { |n| "Chord #{n}" }
    sequence(:description)  { |n| "This is chord #{n}" }
    user

    factory :submitted_chord do
      state 'Submitted'
    end

    factory :published_chord do
      state 'Published'
    end
  end
end
当使用工厂时,我得到以下错误:

1) Key pages viewing keys on the pending keys page for an admin 
     Failure/Error: @admin = FactoryGirl.create(:admin)
     ActiveRecord::RecordInvalid:
       Validation failed: Email has already been taken, Email has already been taken, Username has already been taken
     # ./spec/requests/key_pages_spec.rb:150:in `block (5 levels) in <top (required)>'

  2) Key pages viewing keys on the pending keys page for an admin 
     Failure/Error: let!(:unpublished_key) { FactoryGirl.create(:key) }
     ActiveRecord::RecordInvalid:
       Validation failed: Email has already been taken, Email has already been taken, Username has already been taken
     # ./spec/requests/key_pages_spec.rb:8:in `block (3 levels) in <top (required)>'
1)密钥页查看管理员挂起密钥页上的密钥
失败/错误:@admin=FactoryGirl.create(:admin)
ActiveRecord::RecordInvalid:
验证失败:已发送电子邮件、已发送电子邮件、已发送用户名
#./spec/requests/key\u pages\u spec.rb:150:in‘block(5级)in’
2) 密钥页查看管理员的挂起密钥页上的密钥
失败/错误:让我来!(:未发布的_键){FactoryGirl.create(:键)}
ActiveRecord::RecordInvalid:
验证失败:已发送电子邮件、已发送电子邮件、已发送用户名
#./spec/requests/key\u pages\u spec.rb:8:in'block(3层)in'
对于和弦:

1) Chord pages viewing chords on the pending chords page for an admin 
     Failure/Error: let!(:unpublished_chord) { FactoryGirl.create(:chord) }
     ActiveRecord::RecordInvalid:
       Validation failed: Email has already been taken, Email has already been taken, Username has already been taken
     # ./spec/requests/chord_pages_spec.rb:4:in `block (2 levels) in <top (required)>'

  2) Chord pages viewing chords on the pending chords page for an admin does not redirect to the sign up page
     Failure/Error: let!(:unpublished_chord) { FactoryGirl.create(:chord) }
     ActiveRecord::RecordInvalid:
       Validation failed: Email has already been taken, Email has already been taken, Username has already been taken
     # ./spec/requests/chord_pages_spec.rb:4:in `block (2 levels) in <top (required)>'

  3) Chord pages viewing chords on the pending chords page for an admin 
     Failure/Error: let!(:unpublished_chord) { FactoryGirl.create(:chord) }
     ActiveRecord::RecordInvalid:
       Validation failed: Email has already been taken, Email has already been taken, Username has already been taken
     # ./spec/requests/chord_pages_spec.rb:4:in `block (2 levels) in <top (required)>'
1)和弦页面在管理员的挂起和弦页面上查看和弦
失败/错误:让我来!(:未发布的_chord){FactoryGirl.create(:chord)}
ActiveRecord::RecordInvalid:
验证失败:已发送电子邮件、已发送电子邮件、已发送用户名
#./spec/requests/chord\u pages\u spec.rb:4:in'block(2层)in'
2) 和弦页面在管理员的挂起和弦页面上查看和弦不会重定向到注册页面
失败/错误:让我来!(:未发布的_chord){FactoryGirl.create(:chord)}
ActiveRecord::RecordInvalid:
验证失败:已发送电子邮件、已发送电子邮件、已发送用户名
#./spec/requests/chord\u pages\u spec.rb:4:in'block(2层)in'
3) 和弦页面查看管理员的挂起和弦页面上的和弦
失败/错误:让我来!(:未发布的_chord){FactoryGirl.create(:chord)}
ActiveRecord::RecordInvalid:
验证失败:已发送电子邮件、已发送电子邮件、已发送用户名
#./spec/requests/chord\u pages\u spec.rb:4:in'block(2层)in'
编辑:添加规范

require 'spec_helper'

describe Key do
  let(:user) { FactoryGirl.create(:user) }
  let(:key)  { FactoryGirl.create(:key, user: user) }
  let(:chord)  { FactoryGirl.create(:chord, user: user) }
  let(:chord_2)  { FactoryGirl.create(:chord, user: user) }
  let(:c_chord) { FactoryGirl.create(:chord, user: user) }
  let(:note)  { FactoryGirl.create(:note) }
  let(:note_2) { FactoryGirl.create(:note) }
  let(:note_3) { FactoryGirl.create(:note) }
  let(:note_4) { FactoryGirl.create(:note) }

  subject { key }

  describe "its attributes" do
    it { should respond_to(:name) }

    describe "when name is blank" do
      before { key.name = nil }
      it { should_not be_valid }
    end

    it { should respond_to(:description) }

    describe "when description is blank" do
      before { key.description = nil }
      it { should_not be_valid }
    end

    it { should respond_to(:user_id) }
    it { should respond_to(:user) }
    its(:user) { should eq user }

    describe "when user_id is not present" do
      before { key.user_id = nil }
      it { should_not be_valid }
    end

    it { should be_valid }

    it { should respond_to(:keychords) }
    it { should respond_to(:chords) }

    it { should respond_to(:state) }
    it { should respond_to(:submit) }
    it { should respond_to(:withdraw) }
    it { should respond_to(:approve) }
    it { should respond_to(:unpublish) }

    it { should respond_to(:root_note) }
    it { should respond_to(:accidental) }
    it { should respond_to(:third) }

    describe "when root_note is blank" do
      before { key.root_note = nil }
      it { should_not be_valid }
    end

    describe "when root_note is not in root_noteS" do
      before { key.root_note = 'X' }
      it { should_not be_valid }
    end

    describe "when accidental is blank" do
      before { key.accidental = nil }
      it { should_not be_valid }
    end

    describe "when accidental is not in ACCIDENTALS" do
      before { key.accidental = 'Not an Accidental' }
      it { should_not be_valid }
    end

    describe "when third is blank" do
      before { key.third = nil }
      it { should_not be_valid }
    end

    describe "when third is not in THIRDS" do
      before { key.third = 'Not in Thirds' }
      it { should_not be_valid }
    end
  end

  describe "state changes" do
    it "should change from unpublished to submitted when submitted" do
    expect{ key.submit }.to change{key.state}.from('Unpublished').to('Submitted')
    end

    it "should change from submitted to unpublished when withdrawn" do
      key.state = 'Submitted'
      expect{ key.withdraw }.to change{key.state}.from('Submitted').to('Unpublished')
    end

    it "should change from submitted to approved when approved" do
      key.state = 'Submitted'
      expect{ key.approve }.to change{key.state}.from('Submitted').to('Published')
    end

    it "should change from submitted to unpublished when rejected" do
      key.state = 'Submitted'
      expect{ key.reject }.to change{key.state}.from('Submitted').to('Unpublished')
    end  

    it "should change from published to unpublished when unpublished" do
      key.state = 'Published'
      expect{ key.unpublish }.to change{key.state}.from('Published').to('Unpublished')
    end
  end  

  describe "its chords" do
    it "should not have any chords" do
      key.chords.count.should == 0
    end

    it "should not has_chord?" do
      expect(key.haschord?(chord)).to be_false
    end

    it "add chords should change number of chords" do
      expect { key.addchord!(chord) }.to change{key.chords.count}.from(0).to(1)
    end

    describe "after adding a chord" do
      before { key.addchord!(chord) }

      it "should add the chord" do
        expect(key.chords).to include(chord)
      end

      it "should only have the added chord" do
        expect(key.chords).not_to include(chord_2)
      end

      it "should haschord?(chord)" do
        expect(key.haschord?(chord)).to be_true
      end

      it "should not haschord?(chord2)" do
        expect(key.haschord?(chord_2)).to be_false
      end
    end
  end

  describe "its notes" do
    before do
      chord.notes << [note, note_2]
      chord_2.notes << [note_3, note_4]
    end

    describe "before adding chords" do
      it "should not have any notes" do
        key.notes.count.should == 0
      end

      it "should not hasnote?(note)" do
        expect(key.hasnote?(note)).to be_false
      end
    end

    describe "after adding a chord" do
      before { key.chords << chord }

      it "should have notes" do
        key.notes.count.should == 2
      end

      it "should have notes 1 and 2" do
        expect(key.notes).to include(note, note_2)
      end

      it "should not have notes 3 or 4" do
        expect(key.notes).not_to include(note_3, note_4)
      end

      it "should hasnote?(note)" do
        expect(key.hasnote?(note)).to be_true
      end
    end
  end
end
require'spec\u helper'
描述关键点
let(:user){FactoryGirl.create(:user)}
let(:key){FactoryGirl.create(:key,user:user)}
let(:chord){FactoryGirl.create(:chord,user:user)}
let(:chord_2){FactoryGirl.create(:chord,user:user)}
let(:c_chord){FactoryGirl.create(:chord,user:user)}
let(:note){FactoryGirl.create(:note)}
让(:note_2){FactoryGirl.create(:note)}
让(:note_3){FactoryGirl.create(:note)}
让(:note_4){FactoryGirl.create(:note)}
主题{key}
描述“它的属性”吗
它{应该响应(:name)}
描述“当名称为空”时,请执行以下操作
在{key.name=nil}之前
它{应该是无效的}
结束
它{应该响应(:description)}
说明“说明为空时”是否
在{key.description=nil}之前
它{应该是无效的}
结束
它{应该响应(:user_id)}
它{应该响应(:user)}
它的(:user){should eq user}
描述“当用户id不存在时”是否
在{key.user_id=nil}之前
它{应该是无效的}
结束
它{应该是有效的}
它{应该对(:keychords)作出响应}
它{应该回应(:和弦)}
它{应该响应(:state)}
它{应该响应(:提交)}
它{应该响应(:撤回)}
它{应该响应(:批准)}
它{应该响应(:未发布)}
它{应该响应(:root\u note)}
它{应该响应(:意外)}
它{应该对(:third)作出响应}
描述“当根注释为空时”是否
在{key.root_note=nil}之前
它{应该是无效的}
结束
描述“当根注释不在根注释中时”是否
在{key.root_note='X'}之前
它{应该是无效的}
结束
描述“当意外为空”时,请执行以下操作:
在{key.contractive=nil}之前
它{应该是无效的}
结束
描述“当意外事件不在意外事件中时”是否
在{key.contractive='非意外'}之前
它{应该是无效的}
结束
描述“当第三个是空的”时
在{key.third=nil}之前
它{应该是无效的}
结束
描述“当三分之一不在三分之一时”做什么
在{key.third='不在三分之一'之前}
它{应该是无效的}
结束
结束
描述“状态变化”是什么
它“应该在提交时从未发布更改为已提交”
期望{key.submit}.将{key.state}.从('Unpublished')更改为('Submitted'))
结束
它“撤销时应从已提交更改为未发布”
key.state='Submitted'
期望{key.draw}.将{key.state}.从('Submitted')更改为('Unpublished'))
结束
它“应在批准时从提交更改为批准”吗
key.state='Submitted'
期望{key.approve}.将{key.state}.从('Submitted')更改为('Published'))
结束
它“应在被拒绝时从提交更改为未发布”
key.state='Submitted'
期望{key.reject}.将{key.state}.从('Submitted')更改为('Unpublished'))
结束
它“应该在未发布时从已发布更改为未发布”吗
key.state='Published'
期望{key.unpublish}.将{key.state}.从('Published')更改为('Unpublished'))
结束
结束
描述“它的和弦”吗
它“不应该有任何和弦”吗
key.chords.count.should==0
结束
它“不应该有”和弦吗
期望(key.haschord?(chord)).为假
结束
它“加和弦寿”
config.before(:suite) do
  DatabaseCleaner[:active_record].strategy = :transaction
  DatabaseCleaner.clean_with(:truncation)
end

config.before(:each) do
  DatabaseCleaner.start
end

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