Rails:Factory Girl无法排序

6
我刚开始使用factory girl,遇到了一个关于序列的问题:具体来说,它不能自增。我尝试更改数据库类型,从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'

为什么在这个块内有两个语句?f.sequence(:email) { |n| puts "Email ##{n}"; "factory_#{n}@example.com" } - Nathan Long
你尝试过在运行时执行tail -f log/test.log吗? - Nathan Long
你正在使用autotest吗?另外,在测试运行之间,你使用什么数据库清理策略来清理测试数据库? - Jesse Wolgamott
4个回答

7

使用最新语法

FactoryGirl.define do

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

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

end

4

你的问题不在于Factory Girl。当实例化“user”时,你需要在before块内执行以便为每个要运行的测试创建一个新用户。user变量也应该是实例变量(即前缀为@)。

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

1

你已经单独定义了序列吗?这是我一个工厂文件中的摘录:

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

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

我知道FG支持在工厂中内联定义序列,但最好看看您的问题是否特定于内联语法,或适用于使用旧语法定义的序列。 - Jason Morrison

0

我不确定为什么会发生这种情况,但以下是一些建议:

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

    user = Factory.build(:user)

或者,

  1. 使用一个before each块:

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接