我正在为使用node-cache
的函数编写单元测试。在以下功能中,
cache.get
处返回字符串>cache.get
中的数组[请注意,由于它与我的问题无关,因此我从testFunction中删除了部分代码。
const NodeCache = require('node-cache'); const cache = new NodeCache(); ... const testFunction = () => { let myStringCache = cache.get('cacheName1'); let myArrayCache = cache.get('cacheName2'); ... Do something with caches ... return 'return something'; } ... module.exports = { ..., testFunction, ... }
我创建了以下测试
describe('sample test with testFunction() ', ()=>{ let stubCache; let stub; before((done)=>{ sandbox = sinon.createSandbox(); stubCache = sandbox.stub(cache, 'get'); stubCache.withArgs('cacheName1').returns('sample string'); stubCache.withArgs('cacheName2').returns([1,2,3,4,5,6]); stub = proxyquire('./filelocation.js',{ 'node-cache': stubCache }); done(); }); it('should not throw error',(done)=>{ chai.expect(stub.testFunction()).not.to.throw; }); })
我正在谷歌搜索,有一些使用
proxyquire
存根值的部分解决方案。但看起来确实存在存根,但不是我想要的位置。它在NodeCache
存根,但cache
所以我有问题:
cache.get()
与mocha
,chai
或sinon
存根吗?如果是这样,请分享您的做法? cache.get()
的参数对不同的收益存根? 我正在为使用节点缓存的功能编写单元测试。在下面的函数中,我想在第一个缓存中返回一个字符串。在第二个缓存中获取一个数组。get请注意,我...
这里是单元测试解决方案: