Noah Clark
Noah Clark

Reputation: 8131

Factory Girl: uninitialized constant

I have a factory such as:

FactoryGirl.define do
  factory :page do
    title 'Fake Title For Page'
  end
end

And a test:

describe "LandingPages" do
    it "should load the landing page with the correct data" do
        page = FactoryGirl.create(:page)
        visit page_path(page)
    end
end

My spec_helper.rb contains:

require 'factory_girl_rails' 

and yet I keep getting:

LandingPages should load the landing page with the correct data
     Failure/Error: page = FactoryGirl.create(:page)
     NameError:
       uninitialized constant Page
     # ./spec/features/landing_pages_spec.rb:5:in `block (2 levels) in <top (required)>'

This is a new project, so I don't believe the test is actually the problem. I believe it could be setup incorrectly. Any ideas on things to try and/or where to look to resolve this?

My uneventful pages.rb file:

class Pages < ActiveRecord::Base
  # attr_accessible :title, :body
end

Upvotes: 8

Views: 10896

Answers (2)

dwhalen
dwhalen

Reputation: 1925

Looks like the name of your model is plural: Pages. This should really be singular: Page. You'll need to rename the file to app/models/page.rb as well. FactoryGirl is assuming a singular model name.

Upvotes: 6

Daniel Evans
Daniel Evans

Reputation: 6808

It looks from your file names like the model is actually named LandingPage. The factory is trying to guess your class name based on the name you have given it. So :page becomes Page.

You can change name of the factory or you can add an explicit class option:

FactoryGirl.define do
  factory :landing_page do
    title 'Fake Title For Page'
  end
end

or

FactoryGirl.define do
  factory :page, :class => LandingPage do
    title 'Fake Title For Page'
  end
end

Upvotes: 23

Related Questions