title: Uninitialized constant (NameError) when using FactoryGirl in module
body: <p>Here's the error I'm getting when I try to run my tests with RSpec:</p>
  
  <pre><code>C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/infl
  ector/methods.rb:230:in `block in constantize': uninitialized constant User (Nam
  eError)
  </code></pre>
  
  <p>I'm trying to run FactoryGirl with RSpec but without Rails. Here are the files that take part in the testing:</p>
  
  <p><strong>user_spec.rb</strong></p>
  
  <pre><code>require 'spec_helper'
  
  module Bluereader
    describe User do
      describe 'login' do
        user = FactoryGirl.build(:user)
      end
  
      describe 'logout' do
  
      end
  
      describe 'create_account' do
  
      end
  
      describe 'delete_account' do
  
      end
    end
  end
  </code></pre>
  
  <p><strong>spec/spec_helper</strong></p>
  
  <pre><code>$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..'))
  $LOAD_PATH.unshift(File.dirname(__FILE__))
  
  require 'rspec'
  require 'lib/bluereader'
  
  require 'factory_girl'
  FactoryGirl.find_definitions
  </code></pre>
  
  <p><strong>spec/factories.rb</strong></p>
  
  <pre><code>require 'digest/sha1'
  
  FactoryGirl.define do
    sequence(:username) { |n| "user-#{n}" }
  
    factory :user do
      username
      encrypted_password Digest::SHA1.hexdigest('password')
      full_name 'John Doe'
      logged_in_at Time.now
      logged_out_at 0
    end
  end
  </code></pre>
  
  <p>At this point I know that the <code>factories.rb</code> file is being loaded (I tried with the moronic print-debugging). When I remove the <code>user = FactoryGirl.build(:user)</code> line from <strong>user_spec.rb</strong> I get no errors (and the normal RSpec feedback telling me there are no tests, but no errors). If you are interested, here's my model:</p>
  
  <p>require 'digest/sha1'</p>
  
  <pre><code>module Bluereader
    class User &lt; ActiveRecord::Base
      has_many :categories, :foreign_key =&gt; :user_id
      has_many :news, :foreign_key =&gt; :user_id
      has_many :settings, :foreign_key =&gt; :user_id
  
      attr_reader :full_name
  
      class &lt;&lt; self
        def login(username, password)
          encrypted_password = Digest::SHA1.hexdigest(password)
  
          if not User.exists?(:username =&gt; username, :encrypted_password =&gt; encrypted_password)
            user_id = User.id_from_username(username)
            update(user_id, :logged_in_at =&gt; Time.now, :logged_out_at =&gt; 0)
          end
        end
  
        def logout
          update(current_user.id, :logged_out_at =&gt; Time.now)
        end
  
        def validate_account(username, password, full_name)
          if username.empty? or password.empty or full_name.empty?
            return 'Please fill in all the fields.'
          end
  
          if User.exists?(:username =&gt; username)
            return 'That username is already in use.'
          end
  
          unless username =~ /^\w+$/
            return 'Username field should contain only letters, numbers and underscores.'
          end
  
          ''
        end
  
        def create_account(username, password, full_name)
          encrypted_password = Digest::SHA1.hexdigest(password)
          User.create(:username =&gt; username,
                      :encrypted_password =&gt; encrypted_password,
                      :full_name =&gt; full_name,
                      :logged_in_at =&gt; Time.now,
                      :logged_out_at =&gt; 0)
        end
  
        def delete_account
          current_user.destroy
        end
  
        private
        def id_from_username(username)
          user = where(:username =&gt; username).first
          user.nil? ? 0 : user.id
        end
  
        def current_user
          where(:logged_out_at =&gt; 0).first
        end
      end
    end
  end
  </code></pre>
  
  <p><strong>SOLUTION</strong></p>
  
  <p>The problem was that the class User was in a module, here's the solution:</p>
  
  <p><code>factory :user, class: Bluereader::User do</code></p>