Ruby:有没有办法只模拟类的现有方法,而不是全部?

Ruby:有没有办法只模拟类的现有方法,而不是全部?,ruby,interface,mocking,Ruby,Interface,Mocking,有没有办法只模拟类的现有方法(参数计数正确),而不是全部?我想测试我是否使用了具有正确参数计数的现有方法,即使对象被模拟 我使用Ruby1.9.3和mocha(0.11.1)进行测试 class A def a return 1 end end ... def test_... object = A.new # or object = mock( '::A' ) object.expects( :a ) # ok object.expects( :b ) #

有没有办法只模拟类的现有方法(参数计数正确),而不是全部?我想测试我是否使用了具有正确参数计数的现有方法,即使对象被模拟

我使用Ruby1.9.3和mocha(0.11.1)进行测试

class A
  def a
    return 1
  end
end

... 

def test_...
  object = A.new # or object = mock( '::A' )
  object.expects( :a ) # ok

  object.expects( :b ) # I want "no method error" to be raised...
  object.stubs( :c ) # And here too
end
答案是

Mocha::Configuration.prevent(:stubbing_non_existent_method)
附言

p.p.S.让我自己用谷歌搜索一下:(

另一种方法是使用“回应式”(也称为“江湖郎中式”)的方法: