【问题标题】:Fabrication gem with associations undefined method error具有关联未定义方法错误的制造宝石
【发布时间】:2013-05-20 06:48:01
【问题描述】:

我已经开始使用带有 RSpec 的 Fabrication gem 并且总体上取得了巨大的成功。但是,我似乎无法让关联制造工作。

我正在使用 Ruby 1.9.3、Rails 3.2.12、RSPec 2.13.0 和 Fabrication 2.7.0

型号

class RedistributionSale < ActiveRecord::Base

  belongs_to :account
  belongs_to :customer
  has_many :red_sale_itemisations, :dependent => :destroy
  has_many :materials, :through => :red_sale_itemisations


class RedSaleItemisation < ActiveRecord::Base
  belongs_to :redistribution_sale
  belongs_to :material

制造商

Fabricator(:redistribution_sale) do
  invoice_number { sequence(:invoice_number) { |i| i } } 
  status "Sales Receipt" 
end

Fabricator(:red_sale_itemisation) do
  quantity 1 
  material_id 1
  redistribution_sale_id 1 
end

此时我可以独立制作这两个模型中的任何一个。但我想同时构建它们来测试所有模型代码

我有以下测试

it "returns correct unit prices with" do

  material_1 = Fabricate(:material, l1price: 7.7, l2price: 8.8, discount: false)

  redistribution_sale = Fabricate(:redistribution_sale, ) do
    red_sale_itemisations { Fabricate(:red_sale_itemisation, material_id: material_1.id, quantity: 2 ) }
  end
  expect(redistribution_sale.total_value).to eq 17.6

end

但是,我收到以下错误

Failures:

  1) RedistributionSale calculated fields returns correct unit prices with
     Failure/Error: redistribution_sale = Fabricate(:redistribution_sale) do
     NoMethodError:
       undefined method `each' for #<RedSaleItemisation:0x00000008308d58>
     # ./spec/models/redistribution_sale_spec.rb:87:in `block (3 levels) in <top (required)>'

这是以前见过的,还是我做错了什么?

迈克尔

【问题讨论】:

    标签: ruby-on-rails-3.2 rspec-rails fabrication-gem


    【解决方案1】:

    我认为这是制造中的错误。您现在可以通过将 red_sale_itemisations 的内容包装在一个数组中来解决此错误,如下所示:

    it "returns correct unit prices with" do
    
      material_1 = Fabricate(:material, l1price: 7.7, l2price: 8.8, discount: false)
    
      redistribution_sale = Fabricate(:redistribution_sale, ) do
        red_sale_itemisations { [Fabricate(:red_sale_itemisation, material_id: material_1.id, quantity: 2 )] }
      end
      expect(redistribution_sale.total_value).to eq 17.6
    
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-01
      相关资源
      最近更新 更多