user1120144
user1120144

Reputation:

Uninitialized constant (NameError) when using FactoryGirl in module

Here's the error I'm getting when I try to run my tests with 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'm trying to run FactoryGirl with RSpec but without Rails. Here are the files that take part in the testing:

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

spec/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

spec/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 the factories.rb file is being loaded (I tried with the moronic print-debugging). When I remove the user = FactoryGirl.build(:user) line from user_spec.rb 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:

require '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

SOLUTION

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

factory :user, class: Bluereader::User do

Upvotes: 4

Views: 2747

Answers (3)

stevec
stevec

Reputation: 52797

For anyone clumsy like me, you may have FactoryGirl in your code where you meant to have FactoryBot

Upvotes: 0

Stefan Collier
Stefan Collier

Reputation: 4682

Taken from the bottom of the question

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

factory :user, class: Bluereader::User do

Upvotes: 1

Peter Brown
Peter Brown

Reputation: 51717

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

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

Update

Even if you're not using Rails, you'll still need to require the models in your spec helper.

Upvotes: 1

Related Questions