Toontje
Toontje

Reputation: 1485

How to setup factory_girl_rails with Rails 5.0.1

I just setup a new Rails project with Rails 5.0.1

I added factory_girl_rails to the development and test group in the Gemfile.

Then I created a article factory in the spec/factories folder:

FactoryGirl.define do
  factory :article do
    title { Faker::Lorem.sentence }
    body { Faker::Lorem.paragraph }
  end
end

my feature spec looks like this:

require 'rails_helper'

feature "guest views articles" do

  let(:article1) { FactoryGirl.create(article) }
  let(:article2) { FactoryGirl.create(article) }

  scenario "by visiting index page" do
    visit articles_path
    expect(page).to have_content article1.title
    expect(page).to have_content article2.title
  end
end

However, when I run the spec, I get this error message:

Failures:                                                                                

  1) guest views articles by visiting index page                                         
     Failure/Error: let(:article1) { FactoryGirl.create(article) }                       

     NameError:                                                                          
       undefined local variable or method `article' for #    <RSpec::ExampleGroups::GuestViewsArticles:0x007fe957fad078>                                                              
   Did you mean?  article2                                                           
                  article1                                                           
   # ./spec/features/guest_views_articles_spec.rb:5:in `block (2 levels) in <top (required)>'                                                                                   
   # ./spec/features/guest_views_articles_spec.rb:10:in `block (2  levels) in <top (required)>'

from the error message, it seems like the article factory is not defined or recognized. Did I forgot something when setting up the article factory?

my repository is at:

https://github.com/acandael/personalsite/tree/first_test

thanks for your help,

Anthony

Upvotes: 0

Views: 369

Answers (1)

Toontje
Toontje

Reputation: 1485

found the issue:

let(:article1) { FactoryGirl.create(article) }

has to be

let(:article1) { FactoryGirl.create(:article) }

Upvotes: 1

Related Questions