简体   繁体   中英

uninitialized constant User (NameError)

I have a gem where I have moved the core parts of all the models from my main app into modules that are essentially mixins or "concerns". Thus allowing me to use those mixins from the gem in models of various applications and thus allowing me to replicate the tables and relationships across multiple applications.

I was testing this code to make sure that things worked as expected, and I ran into some issues. If we look at the rails_helper , we can see that I am loading factory girl and some other stuff (this is just the top part of the rails_helper.rb - I can post the rest if its really relevant)

ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../dummy/config/environment", __FILE__)
require 'rspec/rails'
require 'factory_girl_rails'

ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => 'test.sqlite3')
load(File.dirname(__FILE__) + "/schema.rb")

Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }

One thing to note is that I am loading database based off an existing schema and using sqlite3 with no credentials - because its just for tests - and I'm loading factory girl and the support directory.

So if we look at the support directory I have two files:

factories.rb

FactoryGirl.define do
  sequence :user_email do |n|
    "user#{n}@example.com"
  end

  # Allows for multiple user names
  sequence :user_name do |n|
    "user#{n}"
  end

  sequence :permission_name do |n|
    "can_read#{n}"
  end

  sequence :role_name do |n|
    "Member#{n}"
  end


  factory :user, :class => User do
    first_name 'Adam'
    last_name 'Something'
    user_name {generate :user_name}
    email {generate :user_email}
    password 'somePasswordThat_Is$ecure10!'
  end
end

model_classes.rb

This support file is used to set up all the models that use the mixins and to help "replicate" the applications tables and relationships for testing but at a more global level, right now I am testing the user model so I only have the user model in here, later on ill have more:

require 'core_models/models/user'

class User < ActiveRecord::Base
  include CoreModels::Models::User
end

With the support files loaded, its time we write a test that requires the use of factory girl.

  context "Validation checks" do
    it "Validate a valid user object" do
     FactoryGirl.build(:user).should be_valid
    end
  end

We just want to build a user object and it should be valid. All the validations, relationships and so on are handled in CoreModels::Models::User , so were just testing that the functionality there still works as intended.

The issue is when I run this test:

$ bin/rspec
-- enable_extension("plpgsql")
   -> 0.0018s
-- create_table("friendly_id_slugs", {:force=>true})
   -> 0.0045s
-- add_index("friendly_id_slugs", ["slug", "sluggable_type", "scope"], {:name=>"index_friendly_id_slugs_on_slug_and_sluggable_type_and_scope", :unique=>true, :using=>:btree})
   -> 0.0016s
-- add_index("friendly_id_slugs", ["slug", "sluggable_type"], {:name=>"index_friendly_id_slugs_on_slug_and_sluggable_type", :using=>:btree})
   -> 0.0019s
-- add_index("friendly_id_slugs", ["sluggable_id"], {:name=>"index_friendly_id_slugs_on_sluggable_id", :using=>:btree})
   -> 0.0026s
-- add_index("friendly_id_slugs", ["sluggable_type"], {:name=>"index_friendly_id_slugs_on_sluggable_type", :using=>:btree})
   -> 0.0028s
-- create_table("group_memberships", {:force=>true})
   -> 0.0050s
-- create_table("groups", {:force=>true})
   -> 0.0027s
-- add_index("groups", ["slug"], {:name=>"index_groups_on_slug", :using=>:btree})
   -> 0.0013s
-- create_table("groups_roles", {:force=>true})
   -> 0.0027s
-- create_table("permissions", {:force=>true})
   -> 0.0028s
-- add_index("permissions", ["slug"], {:name=>"index_permissions_on_slug", :using=>:btree})
   -> 0.0018s
-- create_table("roles", {:force=>true})
   -> 0.0035s
-- add_index("roles", ["slug"], {:name=>"index_roles_on_slug", :using=>:btree})
   -> 0.0016s
-- create_table("roles_permissions", {:force=>true})
   -> 0.0026s
-- create_table("users", {:force=>true})
   -> 0.0026s
-- add_index("users", ["slug"], {:name=>"index_users_on_slug", :using=>:btree})
   -> 0.0016s
-- initialize_schema_migrations_table()
   -> 0.0045s
/Users/Adam/Documents/Rails-Projects/core_models/spec/support/factories.rb:20:in `block in <top (required)>': uninitialized constant User (NameError)
    from /Users/Adam/Documents/Rails-Projects/core_models/.bundle/gems/gems/factory_girl-4.5.0/lib/factory_girl/syntax/default.rb:49:in `instance_eval'
    from /Users/Adam/Documents/Rails-Projects/core_models/.bundle/gems/gems/factory_girl-4.5.0/lib/factory_girl/syntax/default.rb:49:in `run'
    from /Users/Adam/Documents/Rails-Projects/core_models/.bundle/gems/gems/factory_girl-4.5.0/lib/factory_girl/syntax/default.rb:7:in `define'
    from /Users/Adam/Documents/Rails-Projects/core_models/spec/support/factories.rb:1:in `<top (required)>'
    from /Users/Adam/Documents/Rails-Projects/core_models/spec/rails_helper.rb:11:in `block in <top (required)>'
    from /Users/Adam/Documents/Rails-Projects/core_models/spec/rails_helper.rb:11:in `each'
    from /Users/Adam/Documents/Rails-Projects/core_models/spec/rails_helper.rb:11:in `<top (required)>'
    from /Users/Adam/Documents/Rails-Projects/core_models/spec/models/user_spec.rb:1:in `require'
    from /Users/Adam/Documents/Rails-Projects/core_models/spec/models/user_spec.rb:1:in `<top (required)>'
    from /Users/Adam/Documents/Rails-Projects/core_models/.bundle/gems/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `load'
    from /Users/Adam/Documents/Rails-Projects/core_models/.bundle/gems/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `block in load_spec_files'
    from /Users/Adam/Documents/Rails-Projects/core_models/.bundle/gems/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `each'
    from /Users/Adam/Documents/Rails-Projects/core_models/.bundle/gems/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `load_spec_files'
    from /Users/Adam/Documents/Rails-Projects/core_models/.bundle/gems/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:96:in `setup'
    from /Users/Adam/Documents/Rails-Projects/core_models/.bundle/gems/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:84:in `run'
    from /Users/Adam/Documents/Rails-Projects/core_models/.bundle/gems/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:69:in `run'
    from /Users/Adam/Documents/Rails-Projects/core_models/.bundle/gems/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:37:in `invoke'
    from /Users/Adam/Documents/Rails-Projects/core_models/.bundle/gems/gems/rspec-core-3.1.7/exe/rspec:4:in `<top (required)>'
    from bin/rspec:16:in `load'
    from bin/rspec:16:in `<main>'

So as you can see it loads the database and then boom it dies, because it has no idea what User is(?)

So my question is:

Do I have a loading issue where factory is loaded before model_clases? It would make sense to load them alphabetically. If thats the case would customizing there load ordering by doing:

require 'support/model_classes.rb'
require 'support/factories.rb'

fix the issue so that the model classes are created before the factories are?

Dir[] provides no guarantee about the ordering - results will in general by file system dependant. It could be alphabetical it could be the raw unicode binary ordering, or some order that corresponds to some internal data structure.

If you want things returned in a specific order the it's up to you to implement that as you suggest at the end of your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM