2010-12-20 47 views
6

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

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

/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 "[email protected]" 
    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' 
+0

为什么你有这样的块中的两个语句? f.sequence(:email){| n |放入“Email ## {n}”; “factory_#{n}@example.com”} – 2010-12-20 19:29:45

+0

你运行过'tail -f log/test.log'吗? – 2010-12-20 19:31:18

+0

你使用自动测试?另外,你使用什么数据库清理策略来清理运行之间的测试数据库? – 2010-12-20 21:53:17

回答

4

你的问题不是与工厂的女孩。在实例化“用户”时,您需要从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 "[email protected]" 
    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 
+0

(我知道FG支持在工厂内嵌入定义序列,但很高兴看到你的问题是特定于内联语法,还是适用于使用旧语法定义的序列。) – 2010-12-20 23:15:25

0

我不知道为什么会发生,但这里有几个建议:

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

    用户= Factory.build(:用户)

或者,

  1. 使用每个块之前:

    之前(:每个)做 @user =厂(:用户) 端

    (然后将 “用户” 的变量 “@user” )

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