xjq233p_1
xjq233p_1

Reputation: 8070

rspec with factory girl

I have the following factories:

Factory.define :producer, :class => User do |f|
  f.sequence(:email) { |n| "producer_#{n}@shit.com" }
  f.password  "foobar"
  f.password_confirmation "foobar"
  f.role      "producer"
end

Factory.define :business do |f|
  f.sequence(:name) { |n| "business_#{n}" }
  f.association(:producer, :factory => :producer)
end

Factory.define :deal do |d| 
  d.sequence(:title) { |n| "deal_#{n}" }
  d.sequence(:desc) { |n| "deal_desc_#{n}" }
  d.cap "50" 
  d.rate "2" 
  d.start Date.today - 1     # This is the date where you put in db
  d.end Date.today + 7
  d.association :business
end

now when I do the following:

  before(:each) do 
    @consumer = test_sign_in(Factory(:consumer))
    @deal = Factory(:deal)
  end

I am getting an error:

 Failure/Error: @deal = Factory(:deal)
 NoMethodError:
   undefined method `producer=' for #<Business:0x007fb494290090>
 # ./deals_controller_spec.rb:15:in `block (4 levels) in <top (required)>

(Line 15 refers to @deal = Factory(:deal) )

Does anyone know why? I am very new to factory girl and I can't seem to find the documentation explaining association and sequence very well.

Upvotes: 0

Views: 1912

Answers (1)

apneadiving
apneadiving

Reputation: 115541

The problem here is obviously linked to the creation of your producer association.

Since you're using the old dsl, I'd suggest two solutions:

Factory.define :business do |f|
  f.sequence(:name) { |n| "business_#{n}" }
  #try this:
  f.association(:user, :factory => :producer)
  #or this:
  f.after_build { |biz| biz.user = Factory.build(:producer) }
end

The use of after_build or after_create is really a matter of choice, depending on your tests purposes.

Here is a link to the new dsl lookup.

Upvotes: 1

Related Questions