【问题标题】:Rspec 2.14, FactoryGirl 4.2, Rails 3.2.11 - has_many throughRspec 2.14、FactoryGirl 4.2、Rails 3.2.11 - has_many 到
【发布时间】:2013-10-26 02:44:23
【问题描述】:

我是 FactoryGirl 的新手(并且是一般测试),我正在尝试为 has_many 关系建立一个工厂。到目前为止,我发现的所有示例都使用了旧版本的工厂女孩​​,官方文档并没有那么有用。

当我运行测试时,它会说:

Failures:

  1) Brand has a valid factory
     Failure/Error: FactoryGirl.create(:brand).should be_valid
     ActiveRecord::RecordInvalid:
       Validation failed: Styles can't be blank
     # ./spec/models/brand_spec.rb:5:in `block (2 levels) in <top (required)>'

brand.rb

class Brand < ActiveRecord::Base
  attr_accessible               :title,
                                :style_ids

  has_many                      :brand_styles, dependent: :destroy
  has_many                      :styles,       through: :brand_styles

  validates_presence_of         :title
  validates_presence_of         :styles
end

style.rb

class Style < ActiveRecord::Base
  attr_accessible               :title

  has_many                      :brand_styles, dependent: :destroy
  has_many                      :brands,       through: :brand_styles

  validates_presence_of         :title
end

brand_style.rb

class BrandStyle < ActiveRecord::Base
  attr_accessible :brand_id, 
                  :style_id

  belongs_to      :brand
  belongs_to      :style
end

工厂

FactoryGirl.define do
  factory :brand do
    title "Some Brand"

    after(:create) do |brand| 
      brand.style create(:brand_style, brand:brand, style: FactoryGirl.build(:style))
      brand.reload
    end
  end

  factory :style do
    title "Some Style"
  end

  factory :brand_style do
    brand
    style
  end
end

规格

require 'spec_helper'

describe Brand do
  it "has a valid factory" do
    FactoryGirl.create(:brand).should be_valid
  end
end

---编辑---

我根据 Damien Roches 的建议修改了我的工厂,现在出现此错误:

Failures:

  1) Brand has a valid factory
     Failure/Error: FactoryGirl.create(:brand).should be_valid
     NoMethodError:
       undefined method `primary_key' for #<FactoryGirl::Declaration::Implicit:0x007fc581b2d178>
     # ./spec/factories/brand.rb:4:in `block (3 levels) in <top (required)>'
     # ./spec/models/brand_spec.rb:5:in `block (2 levels) in <top (required)>'

改装厂

FactoryGirl.define do
  factory :brand do |brand|
    brand.title "Some Brand"
    brand.styles { build_list(:brand_style, 1, brand: brand, style: build(:style)) }
  end

  factory :style do
    title "Some Style"
  end

  factory :brand_style do
    brand
    style
  end
end

【问题讨论】:

    标签: ruby-on-rails unit-testing rspec factory-bot


    【解决方案1】:

    用这个替换你的 after(:create) 块:

    styles { build_list(:brand_style, 1, brand: brand, style: build(:style)) }
    

    我的factory_girl知识有些粗略,如果你有进一步的问题,请告诉我,有机会我会回复的。

    几个笔记。您不能使用after(:create),因为您的brand 模型需要styles。您不能使用brand.style,因为关系是has_many。那应该是brand.styles。这样,您就不能使用brand.styles = create(),因为赋值需要一个数组。你可以使用brand.styles = [create()],但我更喜欢create_list

    我已将 create(:brand_style) 替换为 build。测试不同的变体,因为我发现有时在您保存父对象时关联不会保存,具体取决于您的配置。详情请见here

    【讨论】:

    • 我已根据您的建议更新了我的代码(以上),现在收到一个新错误 undefined method 'primary_key' for #&lt;FactoryGirl::Declaration::Implicit:0x007fc581b2d178&gt;。有什么想法吗?
    • 你能更新你的:brand 块吗?它应该符合:style:brand_style。不要传入|brand|。使用与其他工厂相同的格式。不能确定这是问题,但请先纠正。如果错误仍然存​​在,则可能是您的railsfactory_girl 版本不兼容。
    • 最初我刚刚按照您指定的方式更新了create(:after) 块,但是,当我运行测试时,它在brand: brand 部分以undefined method 'brand' 失败,这导致我添加了@987654346 @。我会尝试更新其他工厂的格式以使用|whatever| 来消除这种可能性。另外,作为参考,我正在使用 rails 3.2.11
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-27
    • 1970-01-01
    • 2018-03-18
    • 1970-01-01
    相关资源
    最近更新 更多