Sto utilizzando alcuni modelli ActiveRecord
al di fuori di Rails
per cui sto scrivendo test di unità. Nei miei test, mi piacerebbe essere in grado di utilizzare i proiettori e ho pensato di utilizzare alcune delle funzioni di rspec-rails per questo. Non posso semplicemente usare require "rspec-rails"
perché ha alcune dipendenze di Rails e non sto usando Rails.Caricamento di dispositivi ActiveRecord con RSpec senza Rails
Ecco la spec_helper
che sto usando:
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
fine
Ed ecco un esempio di un test
require 'spec_helper'
describe Transaction do
before :each do
self.class.fixtures :users, :merchants
end
it "should do this and that" do
...
end
end
Il problema è che, in questo modo, gli infissi sono non caricato. Ho esaminato il codice rspec-rails
ma non riesco a capire quale modulo dovrei estendere o quale metodo dovrei chiamare.