我有这些文件:文件1.jsvarmod1=require('mod1');mod1.someFunction()...文件2.jsvarFile1=require('./File1');现在在为File2编写单元测试时,是否可以模拟mod1,这样我就不会调用mod1.someFunction()? 最佳答案 我通常使用mockery模块,如下所示:lib/file1.jsvarmod1=require('./mod1');mod1.someFunction();lib/file2.jsvarfile1=require('./file
我有一个类AProvider需要'./b.provider'。constBProvider=require('./b.provider');classAProvider{staticgetdefaultPath(){return`defaults/a/${BProvider.getThing()}`;}}module.exports=AProvider;b.provider.js与a.provider.js相邻,看起来像global.stuff.whatever=require('../models').get('Whatever');//Ididn'twritethis!classB
客户端我用SystemJSstub到模块的路径,就像这样varsystemJsConfig={baseURL:"./",defaultJSExtensions:true,map:{'root-components':'applicationRoot/rootComponents'}};因此require('root-components/foo');将映射到applicationRoot/rootComponents/foo。问题是,如果我在Mocha中使用require('root-components/foo');运行模块,Node不知道该路径的含义。有没有一种明智的方法可以在No
使用proxyquire、sinon和mocha。我能够在第一次调用fetch时stubfetch。但是在递归的第二个获取调用中,我无法断言它。从输出来看,断言可能会在测试完成之前运行。您将在断言后通过secondfetch控制台看到这一点。索引.jsvarfetch=require('node-fetch');functiona(){console.log('functionaruns');fetch('https://www.google.com').then((e)=>{console.log('firstfetch');b();}).catch((e)=>{console.lo
如果我们有三个模块名称A、B和C,那么模块A需要B和B需要C:这个调用会产生什么效果?varA=proxyquire('A',{'C':mockedModule})模块B会得到mock还是真正的C模块? 最佳答案 只会模拟直接依赖。但是您可以嵌套proxyquire语句,因此在您的示例中您可以:constA=proxyquire('../A',{'./B':proxyquire('../B',{'C':mockC})});文件结构是这样的root|--A.js|--B.js`--tests`--A.spec.js并且importC不
我一直在使用Knex成功连接到后端数据库。但我希望能够对我的代码进行单元测试。有没有办法模拟数据库连接?我试过使用proxyquire但我似乎无法让它工作。问题似乎出在Knex的初始化方式上。varknex=require('knex')({client:'mysql',connection:{}});我将knex设置为在我的单元测试中被模拟。myService=proxyquire('../app/myService',{'knex':knexProxy});我的服务包括knex。varknex=require('knex').knex,当我的服务运行查询时,它失败了。varsql=
被测模块:'usestrict';constconfig=require('config');constq=require('q');classRedisAccess{staticgetValue(key){letdeferred=q.defer();if(config.redis.disableInteraction){deferred.resolve();returndeferred.promise;}config.redisClient.get(key,functionhandleResults(err,result){...returndeferred.promise;}}ex
我想用proxyquire模拟MongoDB依赖通过在我的测试中这样做:varproxyquire=require('proxyquire');varcontroller=path.resolve('.path/to/controller/file.js');在每个语句之前:mocked_mongoose={isMocked:true,model:function(name,schema,collection,skipInit){return{find:function(conditions,projection,options,callback){console.log('callb
我在Express中有以下内容//index.jsvarservice=require('./subscription.service');varauth=require('../auth/auth.service');varrouter=express.Router();router.post('/sync',auth.isAuthenticated,service.synchronise);module.exports=router;我想覆盖或模拟isAuthenticated以返回此auth.isAuthenticated=function(req,res,next){retur
我在Express中有以下内容//index.jsvarservice=require('./subscription.service');varauth=require('../auth/auth.service');varrouter=express.Router();router.post('/sync',auth.isAuthenticated,service.synchronise);module.exports=router;我想覆盖或模拟isAuthenticated以返回此auth.isAuthenticated=function(req,res,next){retur