未初始化的常量User(NameError)

时间:2015-02-20 08:16:35

标签: ruby-on-rails ruby gem factory-bot rspec3

我有一个宝石,我将所有模型的核心部分从我的主应用程序移动到基本上是mixins或"关注"的模块中。因此,允许我在各种应用程序的模型中使用gem中的mixins,从而允许我在多个应用程序中复制表和关系。

我正在测试此代码,以确保事情按预期工作,我遇到了一些问题。如果我们查看rails_helper,我们可以看到我正在加载工厂女孩和其他一些东西(这只是rails_helper.rb的顶部 - 我可以发布其余部分,如果它真的相关)< / p>

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的所有模型,并帮助&#34;复制&#34;用于测试的应用程序表和关系,但是在更全局的层面上,现在我正在测试用户模型,所以我在这里只有用户模型,后来生病了更多:

require 'core_models/models/user'

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

加载支持文件后,我们编写一个需要使用工厂女孩的测试时间。

  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是什么(?)

所以我的问题是:

我是否有在loading_clases之前加载工厂的加载问题?按字母顺序加载它们是有意义的。如果是这种情况,那么可以通过以下方式定制负载排序:

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

解决问题,以便在工厂出现之前创建模型类?

1 个答案:

答案 0 :(得分:0)

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

如果您希望以特定顺序返回的内容,则由您在问题结尾处按照您的建议实施。