我正在使用一些我正在为其编写单元测试ActiveRecord
的模型。Rails
在我的测试中,我希望能够使用固定装置,并认为我会为此使用一些rspec-rails功能。我不能简单地使用require "rspec-rails"
,因为它有一些 Rails 依赖项而且我没有使用 Rails。
这是spec_helper
我正在使用的:
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
代码,但无法弄清楚我应该扩展什么模块或应该调用什么方法。