const Client = require('./src/http/client');
module.exports.handler = () => {
const client = new Client();
const locationId = client.getLocationId(123);
};
我如何测试这个模块断言client.getLocationId
已被调用123
茉莉花中的争论?
我知道如何用诗乃实现这一目标,但我对茉莉一无所知。
与Sinon一起你会做什么:
Sinon.spy(client, 'getLocationId');
...
Sinon.assert.calledWith(client.getLocationId, 123);
和茉莉花一起,你会:
spyOn(client, 'getLocationId');
...
expect(client.getLocationId).toHaveBeenCalledWith(123);
Update:所以,你需要的是嘲笑Client
当您正在测试的模块需要时,可以使用该模块。我建议使用代理查询 https://github.com/thlorenz/proxyquire为了这:
const proxyquire = require('proxyquire');
const mockedClientInstance = {
getLocationId: () => {}
};
const mockedClientConstructor = function() {
return mockedClientInstance;
};
const moduleToTest = proxyquire('moduleToTest.js', {
'./src/http/client': mockedClientConstructor
});
这会将您的模拟作为依赖项注入,以便当您正在测试的模块需要时./src/http/client
,它会得到你的模拟而不是真实的Client
模块。之后你只需监视中的方法mockedClientInstance
像平常一样:
spyOn(mockedClientInstance, 'getLocationId');
moduleToTest.handler();
expect(mockedClientInstance.getLocationId).toHaveBeenCalledWith(123);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)