Coffeescript 如何使用Sinon存根super()调用

Coffeescript 如何使用Sinon存根super()调用,coffeescript,sinon,Coffeescript,Sinon,我正在使用Coffeescript,并且正在使用Sinon.js进行测试。在测试调用它所覆盖的方法的方法时,如何存根对super()的调用 例如,我想测试的方法(backbone.js模型): 在本例中,我希望确保使用给定属性调用super(),并且validate返回验证错误super()返回。如下所示: it 'calls super and returns its result', -> whatever = new Whatever() attributes = sinon

我正在使用Coffeescript,并且正在使用Sinon.js进行测试。在测试调用它所覆盖的方法的方法时,如何存根对
super()
的调用

例如,我想测试的方法(backbone.js模型):

在本例中,我希望确保使用给定属性调用
super()
,并且validate返回验证错误
super()
返回。

如下所示:

it 'calls super and returns its result', ->
  whatever = new Whatever()
  attributes = sinon.stub()
  superValidateStub = sinon.mock(Whatever.__super__)
  superValidateStub.expects('validate').withExactArgs(attributes).returns('VALIDATION_RESULT')

  expect(whatever.validate(attributes)).to.eql('VALIDATION_RESULT')

  superValidateStub.verify()

希望这对任何人都有帮助。

你不应该先模拟然后构造吗?@AdrianLang:在这种情况下没有必要,因为
\uuuu super\uuu
无论什么
构造函数的属性,而不是它的原型。
which.validate
中的
super
语句被转换为
which.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuu.validate()
it 'calls super and returns its result', ->
  whatever = new Whatever()
  attributes = sinon.stub()
  superValidateStub = sinon.mock(Whatever.__super__)
  superValidateStub.expects('validate').withExactArgs(attributes).returns('VALIDATION_RESULT')

  expect(whatever.validate(attributes)).to.eql('VALIDATION_RESULT')

  superValidateStub.verify()