2012-10-29 87 views
1

我正在使用minitest與rails 3.2。我想定義我自己的「測試」功能,做一些特殊的事情,然後正常進行。包裝minitest測試塊

通常情況下,你定義的測試是這樣的:

class MyControllertest < ActionController::TestCase       
    test "should note have defined x" do 
    assert(!(defined? x)) 
    get :index 
    assert_response :success 
    end 
end 

我想能夠做到以下

class MyControllerTest < ActionController::TestCase       
    special_test "should define X" do 
    assert(defined? x) 
    get :index 
    assert_response :success 
    end 
end 

所以,我想在一個測試助手,我包括

以下
class ActiveSupport::TestCase  
    def self.special_test(name)                                      
    self.test(name) do                                       
     x=1 
     yield                                          
    end                                            
    end  
end                                     

但是,我得到

undefined method `get' for MyControllerTest:Class 

有人可以幫助教我一些關於元編程/如何去做這件事嗎?

https://github.com/seattlerb/minitest

回答

1

這是在測試例子怪異的原因,你正在展示的測試,sublcass ActionController::TestCase但你定義在ActiveSupport::TestCase你的方法,在ActionController::TestCase定義自定義方法,如果這就是你想要的。

+0

優秀!謝謝 – spike