NameError: uninitialized constant ...

2,541 views
Skip to first unread message

Abbas Mashayekh

unread,
Mar 29, 2012, 8:08:36 AM3/29/12
to factor...@googlegroups.com
Hi, I think I have a problem in configuring/using Factory Girl.

I'm using Rails 3.2 + RSpec + Mongoid + factory_girl_rails. The problem is that when I use, for example, FactoryGirl.build(:valid_user), rake gives me this error:

     NameError:
       uninitialized constant ValidUser

spec/factories.rb:
FactoryGirl.define do

  factory :valid_user do
    name     'username'
    email    'a...@b.com'
    password 'somepassword'
  end
    ...
end

spec/models/user_spec.rb:
require 'spec_helper'

describe User do
  let(:user) { FactoryGirl.build(:valid_user) }
 
  it "should save user with valid field values" do
    user.should be_valid
  end
end

spec/spec_helper.rb:
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  config.infer_base_class_for_anonymous_controllers = false
  config.include FactoryGirl::Syntax::Methods
end

Keenan Brock

unread,
Apr 3, 2012, 9:48:19 AM4/3/12
to factor...@googlegroups.com
Hello Abbas,

You may want to add the class name to your factory definition

factory :valid_user, class: 'User' do

—Keenan
--
Individuals over processes. Interactions over tools. Agile Rails training from thoughtbot, the makers of Clearance, Shoulda, & Factory Girl:
http://thoughtbot.com/services/training
 
You received this message because you are subscribed to the "factory_girl" mailing list.
To post to this group, send email to factor...@googlegroups.com
To unsubscribe from this group, send email to
factory_girl...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/factory_girl?hl=en

str...@bettermistak.es

unread,
Jan 30, 2013, 2:24:24 PM1/30/13
to factor...@googlegroups.com
I got stuck on syntax, as I missed the colon after class the first time I tried using this...

If you want, you can also use pre-1.9 hash rocket syntax like:
factory :valid_user, :class => User do

YMMV,
Strand 
Reply all
Reply to author
Forward
0 new messages