Frage

I have started using the Fabrication gem with RSpec and on the whole have had great success. However, I don't seem to be able to get the association fabrication to work.

I am using Ruby 1.9.3, Rails 3.2.12, RSPec 2.13.0, and Fabrication 2.7.0

Models

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

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

At this point I can fabricate either of those two models independently. But I want to build them at the same time to test all the model code

I have the following test

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

However, I get the following error

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)>'

Is this something that has been seen before, or am I doing something horribly wrong?

Michael

War es hilfreich?

Lösung

This is a bug in fabrication I think. You can get past this error for now by wrapping the contents of the red_sale_itemisations in an array, like so:

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top