如何使用 Minitest 在设置方法中存根?

iro*_*and 5 ruby-on-rails

我如何stub在 中使用方法setup?我只找到了stub像这样的with 块:

class FooTest < ActiveSupport::TestCase
  test 'for_something' do
    Foo.stub :some_method, 3 do
      #assert_equal
    end
  end  
end
Run Code Online (Sandbox Code Playgroud)

但我想存根所有测试。我怎样才能存根?

les*_*est 8

#run您可以通过覆盖测试用例中的方法来实现这一点:

class FooTest < ActiveSupport::TestCase
  def run
    Foo.stub :some_method, 3 do
      super
    end
  end

  test 'for_something' do
    #assert_equal
  end  
end
Run Code Online (Sandbox Code Playgroud)

这是引入需要在每个测试用例“周围”执行的代码的常见方法。