Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 700
Noah Clark Avatar asked Jan 18 '13 00:01

Noah Clark


2 Answers

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
like image 76
Daniel Evans Avatar answered Nov 18 '22 10:11

Daniel Evans


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.

like image 6
dwhalen Avatar answered Nov 18 '22 10:11

dwhalen