2013-01-25 12 views
7

मैं Rails के बाहर कुछ ActiveRecord मॉडल का उपयोग कर रहा हूं जिसके लिए मैं यूनिट परीक्षण लिख रहा हूं। मेरे परीक्षणों में, मैं फिक्स्चर का उपयोग करने में सक्षम होना चाहता हूं और सोचा था कि मैं इसके लिए rspec-rails सुविधाओं का उपयोग करूंगा। मैं बस require "rspec-rails" का उपयोग नहीं कर सकता क्योंकि इसमें कुछ रेल निर्भरताएं हैं और मैं रेल का उपयोग नहीं कर रहा हूं।रेल के बिना RSpec के साथ ActiveRecord जुड़नार लोड हो रहा है

यहाँ 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 

अंत

और यहाँ एक परीक्षण का एक उदाहरण है

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 कोड में देखा लेकिन यह पता नहीं लगा सकता कि मुझे किस मॉड्यूल का विस्तार करना चाहिए या जिस विधि को मुझे कॉल करना चाहिए।

उत्तर

5

सक्रिय रिकॉर्ड कोड में डाइविंग के बाद, मुझे लगा कि फिक्स्चर लोड नहीं किए गए थे क्योंकि मैंने ActiveRecord::Base.configurations सेट नहीं किया था। निम्नलिखित मुद्दे को हल किया गया:

configuration = YAML::load(File.open('config/database.yml')) 
ActiveRecord::Base.configurations = configuration 
संबंधित मुद्दे