2011-01-28 17 views
0

我想寫一個自定義Rake任務來執行一些測試放在lib目錄中的類。這適用於不需要任何模型的基本測試,但我需要使用某些模型進行實際測試。這是我第一次嘗試更高級的rake用法,並且遇到了一些其他的障礙,我遇到了ConnectionNotE建立的錯誤。ConnectionNotEstablished在自定義TestTask錯誤

這裏的耙子任務:

Rake::TestTask.new(:test => 'db:test:prepare') do |test| 
    test.libs << 'test/sync' 
    test.test_files = Dir['test/sync/*_test.rb'] 
    test.verbose = true 
end 

下面是提高ConnectionNotEstablished異常測試:

require 'rubygems' 
require 'app/models/city' 
require 'foo' 
require 'test/unit' 

class SyncTest < Test::Unit::TestCase 
    @@sync = Foo::Sync.new('test') 

    def test_cities 
    assert City.all.size == 2 # the exception is raised at this point 
    @@sync.cities 
    assert City.all.size == 102 
    end 
end 

下面是實際工作的單元測試:

require 'test_helper' 

class CityTest < ActiveSupport::TestCase 
    test "the truth" do 
    assert City.all.size == 2 
    end 
end 

我已經嘗試使用從ActiveSupport::TestCase派生我的測試類而不是Test::Unit::TestCase但它仍然會引發ConnectionNotE已建立的錯誤。我當然做錯了什麼,任何人都可以找到或告訴更好的方式來做到這一點?

回答

0

我終於發現了什麼問題,我已經換成了兩個第一require電話:

require 'test_helper' 

,並加入test目錄到TestTask的庫屬性:

test.libs << 'test'