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

标签 javascript unit-testing express jasmine

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

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/

相关文章:

javascript - 我的代码有什么问题?

json - npm 安装错误 - 无效的 package.json

node.js - 如何在 Node 和 Express 中从 API 调用中获取多个参数

jquery - 直接从客户端访问Mongodb

php - 当我将两个脚本添加到 html 页面时..其中一个不起作用

javascript - RxJS - 对 maxWait 和 maxElements 窗口使用 windowWhen()

javascript - 移动滚动方向检测溢出 : hidden page

Python导入机制和模块模拟

node.js - 运行目录中的所有 'test.html' 文件 - mocha-phantomjs

c# - 使用 Rhino Mocks 测试异步回调