FactoryGirl序列失败

时间:2012-08-04 23:38:50

标签: ruby-on-rails-3 testing factory-bot

  

可能重复:
  Factory Girl sequences not incrementing

FactoryGirl似乎没有正确执行email上的序列,因为当我尝试创建用户6.times时,我遇到了失败。 (另请注意:我在email字段上有唯一性约束。)

FactoryGirl.define do
  factory :user do
    username 'asddfsa'
    sequence(:email) {|n| "asd01#{n}@gmail.com" }
    ...
  end
end

RSpec的:

describe User do
  describe ".matches" do
    before :each do
      6.times { FactoryGirl.create(:user, :gender => 'Female', :seeking => 'Men') }
      4.times { FactoryGirl.create(:user, :gender => 'Male', :seeking => 'Women') }
      ...
    end
    ...

失败:

Failure/Error: 6.times { FactoryGirl.create(:user, :gender => 'Female', :seeking => 'Men') }
     ActiveRecord::RecordInvalid:
       Validation failed: Email has already been taken
     # ./spec/models/user_spec.rb:6:in `block (4 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:6:in `times'
     # ./spec/models/user_spec.rb:6:in `block (3 levels) in <top (required)>'

1 个答案:

答案 0 :(得分:0)

Dean Brundage's question解决了我的问题...

此外,我没有正确配置数据库清理程序,这也是一个错误;)