如何使用Sinon.js监视与被测函数位于同一js文件中的函数 [英] How to spy a function with Sinon.js that is in the same js file as the function under test
问题描述
在尝试窥探与要测试的函数位于同一javascript文件中的函数时,Sinn.js出现问题。此外,我断言该间谍函数仅被调用一次。不幸的是,测试失败。有趣的是,如果spied函数在另一个javascript文件中,而不在被测试的函数中,它将起作用!
I have a problem with Sinon.js while trying to spy a function that is in the same javascript file as the function that I want to test. Furthermore I assert that the spied function is called once. Unfortunately the test fails. Interesting thing is, that if the spied function is in another javascript file than the function under test it works !
这是我的代码:
mock_test.js:
mock_test.js:
var sinon = require('sinon')
var one = require('./one.js')
var two = require('./two.js')
describe('Spy ', function () {
it('Spy another method', sinon.test(function (done) {
var another_method_spy = sinon.spy(one, 'another_method')
one.some_method()
sinon.assert.calledOnce(another_method_spy)
done()
}))
it('Spy second method', sinon.test(function (done) {
var second_method = sinon.spy(two, 'second')
one.call_second()
sinon.assert.calledOnce(second_method)
done()
}))
})
one.js:
var two = require('./two.js')
var some_method = function(){
console.log('one: some method')
another_method()
}
var another_method = function(){
console.log('one: another method')
}
var call_second = function(){
console.log('one: call second')
two.second()
}
module.exports.some_method = some_method
module.exports.another_method = another_method
module.exports.call_second = call_second
two.js:
var second = function(){
console.log('two: second')
}
module.exports.second = second
我无法在互联网上找到任何有帮助的东西,我也尝试了不同的方法。请帮忙,我在这里错过了什么?
I couldn't find anything helpful in the internet and also I tried different things. Please help, what am I missing here ?
欢呼
诺亚
推荐答案
不幸的是,测试失败
Unfortunately the test fails
这是因为一个。 mock_test.js 中的some_method()
在闭包 one中调用
保留了 mock_test.js 中的 one.js 而不是 otherother_method
。some_method() one.another_method
的内容
That is because one.some_method()
in mock_test.js invokes another_method
inside the closure one.some_method()
is holding over the content of one.js and not one.another_method
in mock_test.js.
让我们将one.js重写为:
Lets re-write one.js to:
var a = 'I am exported';
var b = 'I am not exported';
function foo () {
console.log(a);
console.log(this.b)
}
module.exports.a=a;
module.exports.foo=foo;
和模拟_test.js到:
and mock_test.js to:
var one = require('./one');
console.log(one); // { a: 'I am exported', foo: [Function: foo] }
one.a = 'Charles';
one.b = 'Diana';
console.log(one); // { a: 'Charles', foo: [Function: foo], b: 'Diana' }
现在,如果我们调用 one.foo()
,它将导致:
Now if we invoke one.foo()
it will result in:
I am exported
Diana
我被出口了
记录到控制台,因为 foo
中的 console.log(a)
指向<闭包 foo
中的code> var a 保留了 one.js 的内容。
The I am exported
is logged to the console because console.log(a)
inside foo
points to var a
inside the closure foo
is holding over the contents of one.js.
Diana
已记录到控制台,因为 console.log(this.b)
指向 mock_test.js one.b >。 foo
中的
The Diana
is logged to the console because console.log(this.b)
inside foo
points to one.b
in mock_test.js.
您需要更改:
var some_method = function(){
console.log('one: some method')
another_method()
}
至:
var some_method = function(){
console.log('one: some method')
this.another_method()
}
这篇关于如何使用Sinon.js监视与被测函数位于同一js文件中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!