gpt4 book ai didi

javascript - Jasmine 有 createSpy() 返回模拟对象

转载 作者:行者123 更新时间:2023-12-03 08:00:24 25 4
gpt4 key购买 nike

我正在尝试模拟一个响应对象,它看起来像这样:

var res = {
status: jasmine.createSpy().andReturn(this),
send: jasmine.createSpy().andReturn(this)
}

这将返回 jasmine 对象。我真的很想返回包含模拟函数的原始 res 变量。那可能吗?我主要将其实现为包含 res.status().send() 的单元测试函数,这被证明是困难的。

最佳答案

这里的答案实际上很快。调用 andReturn() 会给你 Jasmine 作为'this'。但是,如果您编写 andCallFake(),则该函数将模拟对象视为 this。解决方案如下所示:

status: jasmine.createSpy().and.callFake(function(msg) { return this });

关于javascript - Jasmine 有 createSpy() 返回模拟对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27680933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com