Uninitialized constant (NameError) when using FactoryGirl in module

Here is the error I get when I try to run my tests using RSpec:

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)

      

I am trying to run FactoryGirl with RSpec but without Rails. Here are the files that are being tested:

user_spec.rb

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

      

specs / spec _helper

$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

      

specs /factories.rb

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

      

At this point, I know that a file is loading factories.rb

(I tried with disgusting debugging). When I remove the line user = FactoryGirl.build(:user)

from user_spec.rb , I don't get any errors (and the normal RSpec feedback tells me there are no tests, but no errors). If you're interested, here's my model:

requires "digest / sha1"

module Bluereader
  class User < ActiveRecord::Base
    has_many :categories, :foreign_key => :user_id
    has_many :news, :foreign_key => :user_id
    has_many :settings, :foreign_key => :user_id

    attr_reader :full_name

    class << self
      def login(username, password)
        encrypted_password = Digest::SHA1.hexdigest(password)

        if not User.exists?(:username => username, :encrypted_password => encrypted_password)
          user_id = User.id_from_username(username)
          update(user_id, :logged_in_at => Time.now, :logged_out_at => 0)
        end
      end

      def logout
        update(current_user.id, :logged_out_at => 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 => 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 => username,
                    :encrypted_password => encrypted_password,
                    :full_name => full_name,
                    :logged_in_at => Time.now,
                    :logged_out_at => 0)
      end

      def delete_account
        current_user.destroy
      end

      private
      def id_from_username(username)
        user = where(:username => username).first
        user.nil? ? 0 : user.id
      end

      def current_user
        where(:logged_out_at => 0).first
      end
    end
  end
end

      

Decision

The problem was that the User class was in a module, here is the solution:

factory :user, class: Bluereader::User do

+3


source to share


1 answer


You need to require the rails environment in your auxiliary spec file. Add the following to spec / spec_helper.rb:

require File.expand_path("../../config/environment", __FILE__)

      



Update

Even if you are not using Rails, you will still need to use models in your spec helper.

+1


source







All Articles