Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 加载带有RSpec的ActiveRecord固定装置(无导轨)_Ruby_Unit Testing_Rspec_Rails Activerecord - Fatal编程技术网

Ruby 加载带有RSpec的ActiveRecord固定装置(无导轨)

Ruby 加载带有RSpec的ActiveRecord固定装置(无导轨),ruby,unit-testing,rspec,rails-activerecord,Ruby,Unit Testing,Rspec,Rails Activerecord,我正在使用一些ActiveRecord模型,这些模型在Rails之外,我正在为它们编写单元测试。在我的测试中,我希望能够使用夹具,并认为我会为此使用一些功能。我不能简单地使用require“rspec-rails”,因为它有一些rails依赖项,而我没有使用rails 以下是我正在使用的spec\u帮助程序: require 'active_record' require 'active_record/fixtures' require 'active_support' require 'rsp

我正在使用一些
ActiveRecord
模型,这些模型在
Rails
之外,我正在为它们编写单元测试。在我的测试中,我希望能够使用夹具,并认为我会为此使用一些功能。我不能简单地使用
require“rspec-rails”
,因为它有一些rails依赖项,而我没有使用rails

以下是我正在使用的
spec\u帮助程序

require 'active_record'
require 'active_record/fixtures'
require 'active_support'
require 'rspec/rails/extensions/active_record/base'
require 'rspec/rails/adapters'
require 'rspec/rails/fixture_support'  # Includes ActiveRecord::TestFixtures
                                       # into the RSpec config

require 'my_models'

# Setup database connection
environment = "test"
configuration = YAML::load(File.open('config/database.yml'))
ActiveRecord::Base.establish_connection(configuration[environment])

RSpec.configure do |config|
  config.fixture_path = File.expand_path("../../test/fixtures", __FILE__)

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = "random"

  # Run each test inside a DB transaction
  config.around(:each) do |test|
    ActiveRecord::Base.transaction do
      test.run
      raise ActiveRecord::Rollback
    end
  end
end
这是一个测试的例子

require 'spec_helper'

describe Transaction do

  before :each do
    self.class.fixtures :users, :merchants
  end

  it "should do this and that" do
    ...
  end
end

问题是,通过这种方式,夹具没有加载。我查看了
rspec rails
代码,但无法确定应该扩展哪个模块或调用哪个方法。

在深入研究活动记录代码之后,我发现没有加载夹具的原因是因为我没有设置
ActiveRecord::Base.configurations
。以下内容解决了该问题:

configuration = YAML::load(File.open('config/database.yml'))
ActiveRecord::Base.configurations = configuration