Factory Girl: uninitialized constant
Asked Answered
T

2

8

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
Terraterrace answered 18/1, 2013 at 0:0 Comment(0)
A
6

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.

Apostle answered 18/1, 2013 at 1:1 Comment(3)
You will also get this error when you define a Factory that does not uses a model. In this case you could do something like: factory :page, class: OpenStructCyanine
@AmericoSavinon that was the problem in my case. +1Salzburg
the coment of @AmericoSavinon solved my prob and clarified the concept.. thanks.. :)Scrawny
H
23

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
Hajj answered 18/1, 2013 at 0:14 Comment(2)
I've renamed the file to pages_spec.rb. I'm stilling having issues. I just tested to see if it would work in a test env and I got the following: 1.9.3p362 :003 > FactoryGirl.create(:page) NameError: uninitialized constant PageTerraterrace
This should definitely be the accepted answer. The problem was FactoryGirl trying to guess the class name which for similar multiple factories (of the same class) can lead to funny names and consequently to problems.Is
A
6

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.

Apostle answered 18/1, 2013 at 1:1 Comment(3)
You will also get this error when you define a Factory that does not uses a model. In this case you could do something like: factory :page, class: OpenStructCyanine
@AmericoSavinon that was the problem in my case. +1Salzburg
the coment of @AmericoSavinon solved my prob and clarified the concept.. thanks.. :)Scrawny

© 2022 - 2024 — McMap. All rights reserved.