工厂女孩序列不增加 [英] Factory Girl sequences not incrementing

查看:75
本文介绍了工厂女孩序列不增加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图让FactoryGirl为我生成一些名称,但是顺序似乎并没有增加.

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

# 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

规格输出为

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" 

为什么FactoryGirl不会在我的规范中生成一个名称序列?

Why doesn't FactoryGirl generate a sequence of names in my spec?

推荐答案

可行,这意味着您无法在规范中的任何位置覆盖该名称,因为after build挂钩将始终运行并覆盖任何名称.

That works, bit it will mean that you can't override the name anywhere in the specs, because the after build hook will always run and overwrite any name.

您的原始示例不起作用的原因是,您在定义工厂时调用了序列,而不是在工厂运行时调用了序列.您可以提供一个属性定义块,该块将在每次工厂运行时调用.这样,您就有机会为每个实例生成一个值,而不是为所有实例生成一个值.这是最常用的序列和时间.

The reason your original example doesn't work is that you're invoking the sequence when the factory is defined, rather than when the factory is run. You can provide a block to attribute definitions which will be invoked every time the factory runs. This way, you get a chance to generate a value for each instance, rather than generating one value for all instances. This is most frequently used for sequences and times.

您可以使用以下代码段修复原始示例:

You can fix your original example 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 with the same format, you could also leave out the value entirely 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, that won't work if you need different name formats for different factories.

这篇关于工厂女孩序列不增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆