未初始化的常量用户 (NameError)

uninitialized constant User (NameError)

我有一个 gem,我已将所有模型的核心部分从我的主应用程序移动到本质上是混合或 "concerns" 的模块中。因此,我可以在各种应用程序的模型中使用 gem 中的那些混入,从而允许我跨多个应用程序复制表和关系。

我正在测试此代码以确保一切按预期工作,并且我 运行 遇到了一些问题。如果我们查看 rails_helper,我们可以看到我正在加载 factory girl 和其他一些东西(这只是 rails_helper.rb 的顶部 - 我可以 post 其余部分,如果它非常相关)

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 }

需要注意的一件事是我正在加载基于现有模式的数据库并使用没有凭据的 sqlite3 - 因为它仅用于测试 - 我正在加载工厂女孩和支持目录。

所以如果我们查看支持目录,我有两个文件:

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

此支持文件用于设置所有使用 mixins 的模型,并帮助 "replicate" 应用程序表和关系进行测试,但在更全局的层面上,现在我正在测试用户模型所以我这里只有用户模型,稍后我会有更多:

require 'core_models/models/user'

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

加载支持文件后,是时候编写一个需要使用 factory girl 的测试了。

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

我们只想构建一个用户对象,它应该是有效的。所有验证、关系等都在 CoreModels::Models::User 中处理,因此我们只是测试那里的功能是否仍按预期工作。

问题是当我 运行 这个测试:

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

所以你可以看到它加载了数据库,然后它就死了,因为它不知道 User 是什么(?)

所以我的问题是:

工厂在 model_clases 之前加载是否存在加载问题? 按字母顺序加载它们是有意义的。如果是这样的话,可以通过以下方式自定义加载顺序:

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

修复问题,以便在工厂之前创建模型 类?

Dir[] 不保证排序 - 结果通常取决于文件系统。它可以是字母顺序的,也可以是原始的 unicode 二进制顺序,或者对应于某些内部数据结构的某种顺序。

如果您希望按特定顺序返回事物,则由您按照问题末尾的建议实施。