Factory Girl sequences do not increase

I'm trying to get FactoryGirl to generate some names for me, but the sequence doesn't seem to increase.

# spec/factories/vessel.rb require 'factory_girl' FactoryGirl.define do sequence :vessel_name do |n| "TK42#{n}" end factory :vessel do name FactoryGirl.generate(:vessel_name) vessel_type 'fermenter' volume_scalar 100.0 volume_units 'bbl' end end 

 # spec/models/vessel_spec.rb require 'spec_helper' describe Vessel do context 'working in the factory' do it 'makes a valid vessel' do vessel = FactoryGirl.create(:vessel) vessel.should be_valid, "Invalid vessel #{vessel.valid? || vessel.errors.messages.inspect}" end it 'makes another valid vessel' do vessel = FactoryGirl.create(:vessel) vessel.should be_valid, "Invalid vessel #{vessel.valid? || vessel.errors.messages.inspect}" end end end 

Specification output

 Vessel working in the factory makes a valid vessel makes another valid vessel (FAILED - 1) Failures: 1) Vessel working in the factory makes another valid vessel Failure/Error: vessel = FactoryGirl.create(:vessel) ActiveRecord::RecordInvalid: Validation failed: Name has already been taken # ./spec/models/vessel_spec.rb:13:in `block (3 levels) in <top (required)>' 

 # app/models/vessel.rb class Vessel < ActiveRecord::Base attr_accessible :name, :vessel_type, :volume_scalar, :volume_units validates :name, :presence => true, :uniqueness => true end 

 0 HAL:0 work/nrb-brewery-management % bundle show factory_girl_rails rspec /home/brundage/.rvm/gems/ruby-1.9.3-p0/gems/factory_girl_rails-3.5.0 /home/brundage/.rvm/gems/ruby-1.9.3-p0/gems/rspec-2.11.0 0 HAL:0 work/nrb-brewery-management % rails c test Loading test environment (Rails 3.2.6) 1.9.3p0 :001 > FactoryGirl.generate :vessel_name => "TK422" 1.9.3p0 :002 > FactoryGirl.generate :vessel_name => "TK423" 1.9.3p0 :003 > FactoryGirl.generate :vessel_name => "TK424" 1.9.3p0 :004 > FactoryGirl.generate :vessel_name => "TK425" 

Why doesn't FactoryGirl generate a name sequence in my specification?

+8
ruby-on-rails-3 rspec2 factory-bot
source share
2 answers

This works, a bit will mean that you cannot redefine the name anywhere in the specifications, because the hook after assembly will always start and overwrite any name.

The reason your original example does not work is because you are calling the sequence when the factory is defined, and not when the factory starts. You can provide an attribute definition block that will be called every time the factory starts. Thus, you get the opportunity to generate a value for each instance, rather than generate a single value for all instances. It is most often used for sequences and tenses.

The original example can be fixed with this snippet:

 sequence :vessel_name do |n| "TK42#{n}" end factory :vessel do name { generate(:vessel_name) } vessel_type 'fermenter' volume_scalar 100.0 volume_units 'bbl' end 

If all names can be generated in the same format, you can also completely exclude this value by renaming your sequence:

 sequence :name do |n| "TK42#{n}" end factory :vessel do name vessel_type 'fermenter' volume_scalar 100.0 volume_units 'bbl' end 

However, this will not work if you need different name formats for different factories.

+12
source share

And the answer is:

 require 'factory_girl' FactoryGirl.define do sequence :vessel_name do |n| "TK42#{n}" end factory :vessel do vessel_type 'fermenter' volume_scalar 100.0 volume_units 'bbl' after :build do |v| v.name = FactoryGirl.generate(:vessel_name) end end end 
+2
source share

All Articles