Factory Girl: uninitialized constant

I have a factory, for example:

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

And the 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 do not think that the test is actually a problem. I believe this may be misconfigured. Any ideas on what to try and / or where to look for a solution?

My unprecedented pages.rb file:

 class Pages < ActiveRecord::Base # attr_accessible :title, :body end 
+7
source share
2 answers

It looks like your model name is plural: Pages . It really needs to be singular: Page . You will also need to rename the file in app/models/page.rb FactoryGirl adopts a unique model name.

+6
source

It looks like your file names are similar to the LandingPage model. factory tries to guess the name of your class based on the name you gave it. So: a page becomes a page.

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

 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 
+22
source

All Articles