factorygirl rails, says "top required" in my spec - don't know how to fix

Posted by user924088 on Stack Overflow See other posts from Stack Overflow or by user924088
Published on 2012-12-01T21:37:30Z Indexed on 2012/12/01 23:04 UTC
Read the original article Hit count: 526

Filed under:
|

I get the following error message when I run my tests. It says that the problem is in my lecture_spec, and that the top is required. I don't know if this has something to do with requiring my spec_helper.rb file.

  1) Lecture has a valid factory
     Failure/Error: FactoryGirl.create(:lecture).should be_valid
     NoMethodError:
       undefined method `after_build=' for #<Lecture:0x007fe7747bce70>
     # ./spec/models/lecture_spec.rb:21:in `block (2 levels) in <top (required)>'

My factory looks like the following:

require 'faker'

FactoryGirl.define do   
  factory :question do      
    association :lecture        
    name { Faker::Lorem.words(1) }

    description {Faker::Lorem.words(7)}

    factory :question_one do
      answer 1
    end

    factory :question_two do
      answer 2
    end

    factory :question_three do
      answer 3
    end
  end
end

And this is my lecture_spec file

require 'spec_helper'

describe Lecture do     
  it "has a valid factory" do
    FactoryGirl.create(:lecture).should be_valid    
  end
end

and this is my lecture factory, where I defined the lecture factory.

FactoryGirl.define do
    factory :lecture do
        #association :question
        name        {Faker::Lorem.words(1)}
        description {Faker::Lorem.words(7)}
        soundfile_file_name {Faker::Lorem.words(1)}
        soundfile_content_type {Faker::Lorem.words(3)}
        soundfile_file_size     {Faker::Lorem.words(8)}

        after_build do |question|
            [:question_one, :question_two, :question_three].each do |question|
                association :questions, factory: :question, strategy: :build
            end
        end
    end
end

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about factory-girl