Rails:Factory Girl无法排序

时间:2010-12-20 19:15:43

标签: ruby-on-rails factory-bot

刚刚开始工厂女孩,我遇到了排序问题:具体来说,它没有增加。我已经尝试更改数据库类型,从factory_girl 1.3.2更新到2.0.0.beta1(并且factory_girl_rails从1.0更新到1.1.0.beta1),尝试重新创建数据库,但同样的问题 - 序列不会增加和第一次插入唯一字段后,我收到验证错误。

非常感谢任何帮助。代码&下面的跟踪堆栈:

/spec/models/user.rb

require 'spec_helper'

describe User do   describe "test user factory is correct" do
    user = Factory(:user)

    it "should have an email ending in example.com" do
      #user.email.should match "test2@example.com"
    end

    it "should have a password of foobar" do
      user.password.should == 'foobar'
    end

    it "should have a password confirmation field of foobar" do
      user.password_confirmation.should == 'foobar'
    end
    end

end

/spec/factories/user.rb

Factory.define :user do |f|
    f.sequence(:email) { |n| puts "Email ##{n}"; "factory_#{n}@example.com" }
    f.password 'foobar'
    f.password_confirmation { |p| p.password }
end

跟踪:

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/validations.rb:49:in `save!': Validation failed: Email has already been taken (ActiveRecord::RecordInvalid)
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/attribute_methods/dirty.rb:30:in `save!'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:242:in `block in save!'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:289:in `block in with_transaction_returning_status'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:204:in `transaction'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:287:in `with_transaction_returning_status'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:242:in `save!'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/factory_girl-1.3.2/lib/factory_girl/proxy/create.rb:6:in `result'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:327:in `run'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:270:in `create'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:301:in `default_strategy'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/factory_girl-1.3.2/lib/factory_girl.rb:20:in `Factory'
    from /Users/john/Websites/Rails/InDevelopment/fastermanager/spec/models/user_spec.rb:5:in `block (2 levels) in <top (required)>'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.3.1/lib/rspec/core/example_group.rb:131:in `module_eval'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.3.1/lib/rspec/core/example_group.rb:131:in `subclass'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.3.1/lib/rspec/core/example_group.rb:118:in `describe'
    from /Users/john/Websites/Rails/InDevelopment/fastermanager/spec/models/user_spec.rb:4:in `block in <top (required)>'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.3.1/lib/rspec/core/example_group.rb:131:in `module_eval'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.3.1/lib/rspec/core/example_group.rb:131:in `subclass'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.3.1/lib/rspec/core/example_group.rb:118:in `describe'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.3.1/lib/rspec/core/extensions/object.rb:6:in `describe'
    from /Users/john/Websites/Rails/InDevelopment/fastermanager/spec/models/user_spec.rb:3:in `<top (required)>'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:235:in `load'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:235:in `block in load'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `block in load_dependency'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:596:in `new_constants_in'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `load_dependency'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:235:in `load'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.3.1/lib/rspec/core/configuration.rb:388:in `block in load_spec_files'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.3.1/lib/rspec/core/configuration.rb:388:in `map'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.3.1/lib/rspec/core/configuration.rb:388:in `load_spec_files'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.3.1/lib/rspec/core/command_line.rb:18:in `run'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.3.1/lib/rspec/core/runner.rb:55:in `run_in_process'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.3.1/lib/rspec/core/runner.rb:44:in `run'
    from /Users/john/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.3.1/lib/rspec/core/runner.rb:10:in `block in autorun'

4 个答案:

答案 0 :(得分:7)

使用latest syntax

FactoryGirl.define do

  sequence :email { |n| "test#{n}@email.com" }

  factory :user do
    name          "John Doe"
    email         { FactoryGirl.generate(:email) }
  end

end

答案 1 :(得分:4)

你的问题不是工厂女孩。在实例化“user”时,您需要从before块内部执行此操作,以便为正在运行的每个测试创建新用户。用户变量也应该是一个实例变量(即以@为前缀)

require 'spec_helper'

describe User do   

  describe "test user factory is correct" do

    before(:each) do
      @user = Factory(:user)
    end

    it "should have an email ending in example.com" do
      @user.email.should match "test2@example.com"
    end

    it "should have a password of foobar" do
      @user.password.should == 'foobar'
    end

    it "should have a password confirmation field of foobar" do
      @user.password_confirmation.should == 'foobar'
    end

  end

end

答案 2 :(得分:1)

您是否已单独定义序列?这是我的一个工厂文件的摘录:

Factory.sequence(:email)       {|n| "person#{n}@example.com" }

Factory.define :user do |f|
  f.name                  "John Doe"
  f.email                 { Factory.next :email }
end

答案 3 :(得分:0)

我不确定为什么会这样,但这里有几条建议:

  1. 获取未保存在数据库中的用户:

    user = Factory.build(:user)

  2. 或者,

    1. 在每个块之前使用a:

      之前(:每个)做   @user = Factory(:user) 端

      (然后将“用户”变量更改为“@user”)