【发布时间】:2016-04-26 13:54:51
【问题描述】:
我想知道你是否可以帮助我。我正在使用带有 NodeJS 的 Jasmine 间谍。我正在尝试模拟一个函数,该函数被另一个函数调用。但模拟看起来并不像它正在工作。
api.js
function hello() {
greeting();
}
function greeting() {
console.log("welcome!");
console.log("But instead it prints this!");
}
module.export = {
hello : hello,
greeting : greeting
}
api-spec.js
const api = require("./api")
describe("testing jasmine spies", function() {
it("mocks the greeting", function() => {
spyOn(api, "greeting").and.callFake(function() {
console.log("it should print this, since I am mocking it...")
});
api.hello();
});
});
如您所见,我模拟了greeting,它被hello 调用。因此,当我从规范中调用 hello 时,我希望它调用我的 greeting 函数的模拟版本。但它改为调用实际的实现。
你能帮帮我吗?
【问题讨论】:
-
我在 nodeJS 中遇到了同样的问题,在对等函数上使用
this仍然不会为我触发spyOn模拟
标签: node.js jasmine mocha.js sinon chai