skip87
skip87

Reputation: 529

is there a problem with the way I set up factory girl

I'm following micheal hartl rails tutorial. I'm on chapter 10. is there something wrong with the way I've define the cotent part in factory girl . I'm getting this error from factory girl when I'm calling rspec .

Failure/Error: FactoryGirl.create(:micropost, user: @user, created_at: 1.day.ago)
     NoMethodError:
       undefined method `content=' for #<User:0x0000010343f018>

factories.rb

FactoryGirl.define do
  factory :user do
    sequence(:name)    { |n| "Person #{n}" }
    sequence(:email)   { |n| "person_#{n}@example.com" }
    password "foobar"
    password_confirmation "foobar"

    factory :admin do
      admin true
    end

    factory :micropost do 
       content "Lorem ipsum "
       #association :user
       user
    end
  end 
end

the part that's been called

 before { @user.save }
    let!(:older_micropost ) do 
      FactoryGirl.create(:micropost, user: @user, created_at: 1.day.ago)
    end
    let!(:newer_micropost) do
      FactoryGirl.create(:micropost, user: @user, created_at: 1.hour.ago)
    end
    it " should  have  the right  micropost in the right  order" do 
      @user.microposts.should == [newer_micropost, older_micropost]
    end

Upvotes: 0

Views: 293

Answers (1)

sameera207
sameera207

Reputation: 16619

Get factory :micropost out of factory :user, otherwise it will consider the content as an attribute of user.

This should work:

FactoryGirl.define do
  factory :user do
    sequence(:name)    { |n| "Person #{n}" }
    sequence(:email)   { |n| "person_#{n}@example.com" }
    password "foobar"
    password_confirmation "foobar"

    factory :admin do
      admin true
    end
  end 

  factory :micropost do 
    content "Lorem ipsum "
    association :user
  end

end

Upvotes: 1

Related Questions