包装最小的测试块

时间:2012-10-29 20:54:42

标签: ruby-on-rails ruby

我使用minitest和rails 3.2。我想定义自己的"测试"做一些特殊事情的函数,然后正常进行。

通常,您可以定义如下测试:

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 个答案:

答案 0 :(得分:1)

这是测试示例中的奇怪原因,您正在显示测试哪个子类ActionController::TestCase,但您在ActiveSupport::TestCase中定义了方法,在ActionController::TestCase中定义自定义方法,如果这是什么你想要的。