首页 > 解决方案 > 使用 sinon 存根,但未调用存根

问题描述

如何存根_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

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

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

谢谢!

标签: javascriptnode.jsunit-testingmocha.jssinon

解决方案


var anOutgoingMessage = <OutgoingMessage>{};是问题!它必须是var anOutgoingMessage = new OutgoingMessage();。当然没有end其他的{}。让我发疯


推荐阅读