具有关联未定义方法错误的Fabrication gem

时间:2013-05-20 06:48:01

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

我已经开始在RSpec上使用Fabrication宝石,总的来说取得了很大的成功。但是,我似乎无法使协会制作工作。

我使用的是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)>'

这是以前见过的东西,还是我做了可怕的错误?

迈克尔

1 个答案:

答案 0 :(得分:2)

我认为这是制作中的一个错误。您现在可以通过将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