【问题标题】:Stubbing with sinon but stub not called使用 sinon 存根,但未调用存根
【发布时间】:2021-07-26 09:14:37
【问题描述】:

如何存根_http_outgoing.jshttps://github.com/nodejs/node/blob/master/lib/_http_outgoing.js

我有var anOutgoingMessage = <OutgoingMessage>{}; ,我将它传递给我的API调用Get(anIncomingMessage,anOutcomingMessage),其中Get有一个anOutcomingMessage.end(data)

在我的测试中,我想确认 end 是使用预期数据调用的。

我都试过了

  1. const outgoingMessageSpy = sinon.stub(OutgoingMessage.prototype,"end");

  1. const outgoingMessageSpy = sinon.stub(anOutgoingMessage,"end");

然后我

expect(outgoingMessageSpy.called).to.be.true()

对于 1) outgoingMessageSpy.called 是错误的。 2)我得到Cannot stub non-existent property end

end 在 _http_outgoing.js 中定义为 OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {,根据上面的链接。

使用我期望的参数调用此消息并确认 end 调用的正确方法是什么?

谢谢!

【问题讨论】:

    标签: javascript node.js unit-testing mocha.js sinon


    【解决方案1】:

    var anOutgoingMessage = <OutgoingMessage>{}; 是问题所在! 必须是var anOutgoingMessage = new OutgoingMessage();。当然没有end,否则{}。 把我逼疯了

    【讨论】:

      猜你喜欢
      • 2016-08-09
      • 2014-11-29
      • 2020-12-12
      • 1970-01-01
      • 2017-05-04
      • 1970-01-01
      • 2015-11-28
      • 2018-03-29
      • 1970-01-01
      相关资源
      最近更新 更多