just getting started factory girl, , i've come across problem sequencing: specifically, doesn't increment. i've tried changing database type, updating factory_girl 1.3.2 2.0.0.beta1 (and factory_girl_rails 1.0 1.1.0.beta1), tried recreating database, same problem - sequence won't increment , validation error after first time insert unique field.
any assistance appreciated. code & trace stack below:
/spec/models/user.rb
require 'spec_helper' describe user describe "test user factory correct" user = factory(:user) "should have email ending in example.com" #user.email.should match "test2@example.com" end "should have password of foobar" user.password.should == 'foobar' end "should have password confirmation field of foobar" user.password_confirmation.should == 'foobar' end end end
/spec/factories/user.rb
factory.define :user |f| f.sequence(:email) { |n| puts "email ##{n}"; "factory_#{n}@example.com" } f.password 'foobar' f.password_confirmation { |p| p.password } end
trace:
/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 been taken (activerecord::recordinvalid) /users/john/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/attribute_methods/dirty.rb:30:in `save!' /users/john/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:242:in `block in save!' /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' /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' /users/john/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:204:in `transaction' /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' /users/john/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:242:in `save!' /users/john/.rvm/gems/ruby-1.9.2-p0/gems/factory_girl-1.3.2/lib/factory_girl/proxy/create.rb:6:in `result' /users/john/.rvm/gems/ruby-1.9.2-p0/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:327:in `run' /users/john/.rvm/gems/ruby-1.9.2-p0/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:270:in `create' /users/john/.rvm/gems/ruby-1.9.2-p0/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:301:in `default_strategy' /users/john/.rvm/gems/ruby-1.9.2-p0/gems/factory_girl-1.3.2/lib/factory_girl.rb:20:in `factory' /users/john/websites/rails/indevelopment/fastermanager/spec/models/user_spec.rb:5:in `block (2 levels) in <top (required)>' /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' /users/john/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.3.1/lib/rspec/core/example_group.rb:131:in `subclass' /users/john/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.3.1/lib/rspec/core/example_group.rb:118:in `describe' /users/john/websites/rails/indevelopment/fastermanager/spec/models/user_spec.rb:4:in `block in <top (required)>' /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' /users/john/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.3.1/lib/rspec/core/example_group.rb:131:in `subclass' /users/john/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.3.1/lib/rspec/core/example_group.rb:118:in `describe' /users/john/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.3.1/lib/rspec/core/extensions/object.rb:6:in `describe' /users/john/websites/rails/indevelopment/fastermanager/spec/models/user_spec.rb:3:in `<top (required)>' /users/john/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:235:in `load' /users/john/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:235:in `block in load' /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' /users/john/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:596:in `new_constants_in' /users/john/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `load_dependency' /users/john/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:235:in `load' /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' /users/john/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.3.1/lib/rspec/core/configuration.rb:388:in `map' /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' /users/john/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.3.1/lib/rspec/core/command_line.rb:18:in `run' /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' /users/john/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.3.1/lib/rspec/core/runner.rb:44:in `run' /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'
your issue isn't factory girl. when instantiating "user" need inside before block new user gets created each test being run. user variable should instance variable (i.e. prefixed @)
require 'spec_helper' describe user describe "test user factory correct" before(:each) @user = factory(:user) end "should have email ending in example.com" @user.email.should match "test2@example.com" end "should have password of foobar" @user.password.should == 'foobar' end "should have password confirmation field of foobar" @user.password_confirmation.should == 'foobar' end end end
Comments
Post a Comment