Tag: 中华煤炭

如何在Node.js中模拟一个glob调用

我正在使用Mocha,Chai和Sinon JS为我的Node.js应用程序编写unit testing。 这是我想testing的模块: var glob = require('glob'); var pluginInstaller = require('./pluginInstaller'); module.exports = function(app, callback) { 'use strict'; if (!app) { return callback(new Error('App is not defined')); } glob('./plugins/**/plugin.js', function(err, files) { if (err) { return callback(err); } pluginInstaller(files, callback, app); }); }; 我有一个testing的情况下,当没有应用程序使用.to.throw(Error) 。 但是我不知道如何模拟glob调用。 特别是我想告诉我的testing方法,glob-call返回什么,然后检查,如果pluginInstaller被调用,使用sinon.spy 。 这是我迄今为止的testing: var expect = require('chai').expect, pluginLoader […]

nodejs – testing失败,但callback被调用

我有一个模块,我出口,其中有一个方法editHeroImage我试图用mocha , chai和sinon 。 模块有两个作为参数, connection和queries传递的对象。 这些是mySql对象,其中一个包含到数据库的连接,另一个包含在其单独模块中定义的查询string。 我正在导出并试图testing的expObj是一个“帮手”模块。 我已经成功地testing了这个模块的其他方法,就像我试图testing这个方法一样,但是当我遇到由于某种原因而使用async模块的方法时,我的testing不再像预期的那样工作。 我想知道是否在这种特殊情况下丢失了某些东西,因为我testing了其他模块和方法,这些模块和方法也使用了async ,并没有遇到这种情况。 当我运行testing时,它会logging“HELLO!” 如预期的那样,但是已经调用callbackSpy的断言失败。 我在这里失去了主意! 请帮忙! 到底是怎么回事? 试衣服之间是否会有污染? testing方法: expObj.editHeroImage = function(connection, queries, postId, postData, callback) { async.waterfall([ function(next) { var qString = queries.getSinglePostById(); connection.query(qString, [postId], function(err, results) { if (err) { return next(err); } if (!results.length) { console.log('NO POST FOUND WITH ID ' + postId); […]

用嘲讽和sinon嘲笑一个类的方法

我正在学习使用sinon的节点模块嘲讽进行unit testing。 只使用嘲笑和普通的类,我能够成功地注入一个模拟。 不过我想注入一个sinon stub而不是一个普通的类,但是我遇到了很多麻烦。 我试图嘲笑的课程: function LdapAuth(options) {} // The function that I want to mock. LdapAuth.prototype.authenticate = function (username, password, callback) {} 以下是我在beforeEach()函数中使用的代码: beforeEach(function() { ldapAuthMock = sinon.stub(LdapAuth.prototype, "authenticate", function(username, password, callback) {}); mockery.registerMock('ldapauth-fork', ldapAuthMock); mockery.enable(); }); afterEach(function () { ldapAuthMock.restore(); mockery.disable(); }); 我试图以各种方式来模拟/存根LdapAuth类而没有成功,上面的代码只是最新的版本,不起作用。 所以我只想知道如何用颂歌和嘲弄来成功地嘲笑这个。

使用Sinon-Chai时,失败的testing显示“错误:超过2000ms超时”

我有以下路线(快递),我正在编写一个集成testing。 代码如下: var q = require("q"), request = require("request"); /* Example of service wrapper that makes HTTP request. */ function getProducts() { var deferred = q.defer(); request.get({uri : "http://localhost/some-service" }, function (e, r, body) { deferred.resolve(JSON.parse(body)); }); return deferred.promise; } /* The route */ exports.getProducts = function (request, response) { getProducts() .then(function (data) { response.write(JSON.stringify(data)); […]