Jasmine spyOn NodeJS
Jasmine spyOn NodeJS
我想知道你是否可以帮助我。我在 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
函数的模拟版本。但它会调用实际的实现。
你能帮帮我吗?
请考虑如何测试 this.greeting()
和 greeting()
函数调用的区别。
在全局/window范围内执行
function hello() {
greeting();
}
function greeting() {
console.log('original greeting')
}
api = {
hello: hello,
greeting: greeting
}
describe("jasmine spies on global object", function() {
it("mocks the window.greeting()", function() {
spyOn(window, "greeting").and.callFake(function() {
console.log("stubbed `api.greeting()`")
});
api.hello();
expect(window.greeting).toHaveBeenCalled();
});
});
<link href="//safjanowski.github.io/jasmine-jsfiddle-pack/pack/jasmine.css" rel="stylesheet" />
<script src="//safjanowski.github.io/jasmine-jsfiddle-pack/pack/jasmine-2.0.3-concated.js"></script>
在对象上下文中执行
function hello() {
this.greeting();
}
function greeting() {
console.log('original greeting')
}
api = {
hello: hello,
greeting: greeting
}
describe("jasmine spies on object", function() {
it("mocks the api.greeting()", function() {
spyOn(api, "greeting").and.callFake(function() {
console.log("stubbed `api.greeting()`")
});
api.hello();
expect(api.greeting).toHaveBeenCalled();
});
});
<link href="//safjanowski.github.io/jasmine-jsfiddle-pack/pack/jasmine.css" rel="stylesheet" />
<script src="//safjanowski.github.io/jasmine-jsfiddle-pack/pack/jasmine-2.0.3-concated.js"></script>
我想知道你是否可以帮助我。我在 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
函数的模拟版本。但它会调用实际的实现。
你能帮帮我吗?
请考虑如何测试 this.greeting()
和 greeting()
函数调用的区别。
在全局/window范围内执行
function hello() {
greeting();
}
function greeting() {
console.log('original greeting')
}
api = {
hello: hello,
greeting: greeting
}
describe("jasmine spies on global object", function() {
it("mocks the window.greeting()", function() {
spyOn(window, "greeting").and.callFake(function() {
console.log("stubbed `api.greeting()`")
});
api.hello();
expect(window.greeting).toHaveBeenCalled();
});
});
<link href="//safjanowski.github.io/jasmine-jsfiddle-pack/pack/jasmine.css" rel="stylesheet" />
<script src="//safjanowski.github.io/jasmine-jsfiddle-pack/pack/jasmine-2.0.3-concated.js"></script>
在对象上下文中执行
function hello() {
this.greeting();
}
function greeting() {
console.log('original greeting')
}
api = {
hello: hello,
greeting: greeting
}
describe("jasmine spies on object", function() {
it("mocks the api.greeting()", function() {
spyOn(api, "greeting").and.callFake(function() {
console.log("stubbed `api.greeting()`")
});
api.hello();
expect(api.greeting).toHaveBeenCalled();
});
});
<link href="//safjanowski.github.io/jasmine-jsfiddle-pack/pack/jasmine.css" rel="stylesheet" />
<script src="//safjanowski.github.io/jasmine-jsfiddle-pack/pack/jasmine-2.0.3-concated.js"></script>