0

I'm newbie on rails and i want to test an existing rails project with rspec,shoulda and factory girl.
I try to assign value a variable of a model but i can't.
this is the error;

Failure/Error: @brief.brand_info = 'bla bla bla'
  NoMethodError:
    undefined method `brand_info=' for nil:NilClass
  # ./spec/models/brief_spec.rb:34:in `block (3 levels) in <top (required)>'

you can reach my models and factory via visiting this ...link...

Brief Factory Code;

Factory.define :brief, :class => Brief do |b|
  b.brand_info 'brand info'
  b.duration 14
end

Project Factory Code;

Factory.define :project, :class => Project do |p|
  p.association :owner, :factory => :customer

  p.title 'project title'
  p.description 'project description'
  p.stage :brief_completed
  p.contest_type :standard

  p.brief {|p| p.association(:brief)}
  p.association :project_type, :factory => :project_type
end

Factory.define :project_with_prize, :parent => :project do |p|
  p.prize 5000
  p.stage :prize_determined
end

Factory.define :project_waiting_for_payment, :parent => :project_with_prize do |p|
  p.stage :awaiting_payment
end

Factory.define :project_started, :parent => :project_with_prize do |p|
  p.start Time.zone.now
  p.end Time.zone.now.advance(:days => 14)
  p.stage :started
end

Factory.define :project_started_with_no_brief, :parent => :project_with_prize do |p|
  p.brief nil
  p.start Time.zone.now.advance(:days => -10)
  p.end Time.zone.now.advance(:days => 4)
  p.stage :started
end

Factory.define :project_overdue, :parent => :project_with_prize do |p|
  p.start Time.zone.now.advance(:days => -14)
  p.end Time.zone.now.advance(:minutes => -15)
  p.stage :started
end

Factory.define :project_ended, :parent => :project_with_prize do |p|
  p.start Time.zone.now.advance(:days => -15)
  p.end Time.zone.now.advance(:days => -1)
  p.stage :ended
end

Factory.define :project_revising, :parent => :project_ended do |p|
  p.stage :revising
end

Factory.define :project_delivering_files, :parent => :project_ended do |p|
  p.stage :delivering_files
end

Factory.define :project_completed, :parent => :project_ended do |p|
  p.stage :completed
end


Factory.define :faulty_project, :parent => :project do |p|
  p.title nil
end
Community
  • 1
  • 1
user1609468
  • 113
  • 1
  • 10
  • I don't see the factory at the link. In any case, you should put the factory here instead of linking to it so the question is self-contained. – Dave Newton Aug 19 '12 at 19:06
  • possible duplicate of [how to test a method of models with rspec and factory](http://stackoverflow.com/questions/12023470/how-to-test-a-method-of-models-with-rspec-and-factory) – apneadiving Aug 19 '12 at 19:19
  • and this is not duplicate. this is about an issue and it's about this question how can i compose a test case – user1609468 Aug 19 '12 at 19:42

2 Answers2

2

The failure message contains a couple of clues:

  • undefined method 'brand_info=' for nil:NilClass means the #brand_info= method is being called on an object that does not define it; in this case, nil.
  • Failure/Error: @brief.brand_info = 'bla bla bla' shows the line of code where this is happening. For some reason @brief is nil in your spec, but you haven't posted the spec code so I can't tell you why.

I recommend starting with a simple spec that just uses describe/it (e.g. no before, let, etc.), and do something like:

describe Brief do
  it 'assigns the brand info' do
    brief = Brief.new
    brief.brand_info = 'bla bla bla'
  end
end

Once you've got that working, you can replace Brief.new with a call to factory girl (if you actually need the extra setup Factory girl provides--but know that it's extra logic that will slow your spec down) and you can refactor to a let once you have multiple examples that need to operate on a brief instance.

Myron Marston
  • 21,452
  • 5
  • 64
  • 63
  • this is true but i have to use factory girl gem. Thank you for your advice – user1609468 Aug 20 '12 at 22:26
  • Feel free to use factory girl, then. That wasn't really my point. Still, I often find it useful when facing a problem like this to strip things out to the simplest form so I can isolate what the problem is. – Myron Marston Aug 21 '12 at 21:33
0

Thank you for your advice. I solve this problem with declaring variable in before block(not in setup block).

user1609468
  • 113
  • 1
  • 10