【发布时间】:2019-01-08 15:06:13
【问题描述】:
我无法理解为什么 sinon 间谍对我来说失败了,即使我正在监视的函数确实在我的测试期间被调用(我通过一些简单的控制台日志记录证明了这一点)。
所以说我有以下几点:
index.js
let MyModule = require('./src/index.js');
MyModule = new MyModule();
module.exports = {
DoStuff: MyModule.DoStuff,
doOtherStuff: MyModule.doOtherStuff,
};
src/index.js
const MyModule = function MyModule() {
const self = this;
self.doOtherStuff = function doOtherStuff() {
console.log('doOtherStuff called!!!')
}
self.DoStuff = async function DoStuff() {
const xhr = self.axiosInstance();
await xhr.post()
.then((res) => {
self.doOtherStuff(res.data);
})
.catch((_err) => {
console.log(_err);
});
};
}
module.exports = MyModule;
我的测试如下:
const nock = require('nock');
const sinon = require('sinon');
const MyModule = require('../index.js');
describe('When calling DoStuff succeeds in making the xhr call', () => {
before(() => {
nock(apiHostName)
.post('/some-path')
.reply(200, { foo: 'bar' });
});
it('should call doOtherStuff', async () => {
const spy = sinon.spy(MyModule, 'doOtherStuff');
await MyModule.DoStuff();
sinon.assert.calledOnce(spy);
});
});
我在我的测试运行器输出中看到我的 doOtherStuff 函数输出中的控制台日志,但是测试失败,说间谍被调用了零次。
我想知道这是否归结为我正在测试的代码的异步性质,但我确保在我的测试中使用 async/await。我一定是在做一些愚蠢的事情,我哪里错了?
谢谢
更新
所以我尝试将功能剥离回更基本的东西,现在有以下内容:
const MyModule = function MyModule() {
const self = this;
self.doOtherStuff = function doOtherStuff() {
console.log('doOtherStuff called!!!')
}
self.DoStuff = function DoStuff() {
self.doOtherStuff();
};
}
module.exports = MyModule;
所以这将排除我可能遇到的任何异步/等待问题。
但即使在运行以下简单测试时,间谍也不会被调用:
const MyModule = require('../index.js');
it('should call doOtherStuff', () => {
const spy = sinon.spy(MyModule, 'doOtherStuff');
MyModule.DoStuff();
sinon.assert.calledOnce(spy);
});
如果我监视console.log,那么它就会过去。我这里一定是误会了一个很基本的原理,但我不知道它是什么!
这与我的module.exports 的声明方式有关吗?因此,即使我试图在我的index.js(doOtherStuff: MyModule.doOtherStuff)中监视顶级导出,这并不是在我的测试中调用 DoStuff 时在内部实际调用的内容?
【问题讨论】:
-
看起来你在监视
ALClient,而不是MyModule? -
是的,对不起,当我把这段代码 sn-p 时,这是一个错字。我已经纠正了这个,但原来的问题仍然存在。谢谢
-
您的意思是先创建一个 MyModule 的实例吗?在您的示例中,
MyModule没有DoStuff属性,也没有您可以监视的doOtherStuff属性。使用您显示的代码,您应该看到由 Sinon 抛出的“尝试包装未定义的属性 doOtherStuff”异常:jsfiddle.net/j7ksobce/1。但是,如果您创建MyModule的实例,它可以正常工作:jsfiddle.net/qm0gj65o -
我在测试规范文件开头的模块中使用
require,因此这些属性在测试运行时可用。我没有在我的测试规范 sn-p 中显示 require 语句,我现在添加了它。我怀疑如果您遵循module.exports语法,它可能会开始为您失败。 -
@mindparse 我看到断开连接的位置。在您的第一段示例代码中,您在分配
module.exports = MyModule;之前执行MyModule = new MyModule();。在您的最后一个示例中,您从不调用new MyModule()。你的两个例子根本不同。
标签: node.js mocha.js sinon chai