我有一个宝石,我正在尝试测试一些mixins,这些mixin将用于模型中以确定关系并添加辅助函数,我遇到的问题是当我去测试我的宝石中的这些mixins时我得到:uninitialized constant ActiveRecord (NameError)
。
在我的rails_helper.rb
(rspec 3.1)中,我添加了以下内容:
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3')
load(File.dirname(__FILE__) + "/schema.rb")
在我spec/models/user.rb
我添加了:
describe "Test the user model mixin" do
class User < ActiveRecord::Base
# This is what I am testing
include CoreModels::Models::User
end
context "Fail to find a user" do
it "should return nil on a user not existing" do
User.find_by_first_name('sally').should == nil
end
end
end
当我跑步时:
$ bin/rspec
/Users/Adam/Documents/Rails-Projects/core_models/spec/models/user_spec.rb:4:in `block in <top (required)>': uninitialized constant ActiveRecord (NameError)
from /Users/Adam/Documents/Rails-Projects/core_models/.bundle/gems/gems/rspec-core-3.1.7/lib/rspec/core/example_group.rb:325:in `module_exec'
from /Users/Adam/Documents/Rails-Projects/core_models/.bundle/gems/gems/rspec-core-3.1.7/lib/rspec/core/example_group.rb:325:in `subclass'
from /Users/Adam/Documents/Rails-Projects/core_models/.bundle/gems/gems/rspec-core-3.1.7/lib/rspec/core/example_group.rb:219:in `block in define_example_group_method'
from /Users/Adam/Documents/Rails-Projects/core_models/.bundle/gems/gems/rspec-core-3.1.7/lib/rspec/core/dsl.rb:41:in `block in expose_example_group_alias'
from /Users/Adam/Documents/Rails-Projects/core_models/.bundle/gems/gems/rspec-core-3.1.7/lib/rspec/core/dsl.rb:79:in `block (2 levels) in expose_example_group_alias_globally'
from /Users/Adam/Documents/Rails-Projects/core_models/spec/models/user_spec.rb:2: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>'
我是否需要要求有效记录?
答案 0 :(得分:2)
确保您在spec/rails_helper.rb
:
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__) # LOADS Rails env
require 'rspec/rails'
如果您想看到干净的spec/rails_helper.rb
,请使用提供的生成器重新创建rails_helper
文件:rails generate rspec:install
。