Tag: sinon

如何用process.nextTick进行testing

我正在使用摩卡来testing一些Node.js代码,并希望使用process.nextTick()来调用方法的callback。 代码 @getNouns: (callback) -> @_wordnik.randomWords( includePartOfSpeech: 'noun', (e, result) -> throw new Error('Wordnik Failed') if e process.nextTick -> callback(result) ) 考试 it 'should call a callback with the response', (done) -> sinon.stub(Word._wordnik, 'randomWords').yields(null, [ {id: 1234, word: "hello"}, {id: 2345, word: "foo"}, {id: 3456, word: "goodbye"} ] ) spy = sinon.spy() Word.getNouns (result) -> […]

我怎样才能解决这个Q.denodifytesting?

我正在使用一个数据库库,其基于callback的界面如下所示: var DB = { insert: function(options, callback) { } } 我想实现一个包装这个数据库来将其callback风格API转换为基于承诺的API。 要做到这一点,我已经定义了以下类: var DatabaseWrapper = { init: function(db) { this.db = db; }, insert: function(options) { return Q.denodeify(this.db.insert.bind(this.db))(options); } } 我想写一个unit testing,以确保当我调用DatabaseWrapper.insert它调用DB.insert 。 到目前为止,我的testing如下所示: describe('DatabaseWrapper', function () { var wrapper, insertSpy, bindStub; beforeEach(function () { wrapper = Object.create(DatabaseWrapper); insertSpy = sinon.spy(function () { console.log('insertSpy […]

我如何testing具有随机值的属性的对象?

我正在写一个unit testing,我正在嘲笑一个对象(客户端)有一个_request方法,期望一个对象和callback函数。 对象参数有一些随机值的属性: var clientMock = sandbox.mock(client); // client is defined up somewhere clientMock .expects('_request') .withArgs({ method: 'POST', form: { commands: [{ type: "item_add", temp_id: '???', // <== This is random value uuid: '???', // <== Another random value args: { … } }] } }, sinon.match.func); 我如何设置一个testing呢? 或者,我怎么能忽略这些特定的属性,并testing其他? 谢谢。

nodejs上的sinon.js:require('sinon')返回空对象

我想在node.js和mocha上使用sinon 。 我在我的testing文件中做以下事情: var sinon = require('sinon') 之后, sinon var是空的对象。 global.sinon是未定义的。 我究竟做错了什么?

如何模拟来自http.request(nodejs,jasmine,sinon)的响应

我写了一个小的node模块,使得一个http请求,我有麻烦testing它。 有问题的代码如下所示: module.exports = (function () { var http = require("http"), Promise = require("promise"); var send = function send(requestOptions, requestBody) { return new Promise(function (resolve, reject) { http.request(requestOptions, function (response) { var responseChunks = []; response.on('data', function (chunk) { responseChunks.push(chunk); }); response.on('end', function () { resolve(Buffer.concat(responseChunks)); }); response.on('error', function (e) { reject(e); }); }).end(requestBody); }); […]

Sinon.js在一个对象内部存储一个匿名函数

所以我正在testing我的代码,我面临一个testing在unit testing函数内部被调用的匿名函数的问题。 为什么我没有让匿名函数成为一个名为function的实例,是因为我需要closures一些匿名函数中使用的unit testing函数的variables。 举例说明: function funcBeingTested(done, arg1) { var self = this; … self.array.push({ "arguments": [ "x", "y", function (error, result) { // do some stuff with arg1… done(error, arg1); }, "z" ] }); … self.run(); // Eventually will call the anonymous function above from 'self.array' } 现在,我的问题是有没有一种方法来testing使用sinon.js匿名函数内部发生了什么? 有人可以用特定的参数作为funcBeingTested()的unit testing的一部分来调用它? 编辑:为了清除任何我想要实现的误解,我希望能够unit testing匿名函数它自己,并涵盖所有可能的分支机构,以获得100%的覆盖率。 我已经知道如何检查done()callback是否被调用,以及使用sinon.js的参数,问题是我将如何在隔离中testing匿名函数,并用特定的参数调用它以涵盖所有可能的分支。

节点js做方法链的testing存根

我的function是sendMail我想存根functionmailjet,它有一个方法链mailjet.post('发送')。请求… 我想断言callback被称为邮件成功或失败。 那么我如何存根这个方法链? var sendMail = function (templateName, callback) { // From template name find template id of mailjet mailingExternalTemplateModel.findMailingTemplateId(templateName, function (err, result) { const request = mailjet .post("send") .request(params) request .then((result) => { if (typeof callback === 'function') { callback(null, result.body); } }) .catch((err) => { if (typeof callback === 'function') { callback(err, null); […]

如何unit testingmongoose模型?

所以我一整天都想自己找出来。 我发现了一些提示,但这还不够。 我想做一个查询和操作的结果。 问题是,我用过的库不允许,因为它返回一个string或对象格式。 它不模拟结果。 或者至less我不能这样做。 我的代码: •控制器: const UserMock = sinon.mock(User) const expectedResult = { "_id" : "58cc67ab9b11ec4cfd9ebb6e", "email" : "test@email.com", "password" : "$2a$10$3Oka.IuS/xoGJ4CgxWOPVerE.IVvKemsZchegvwsxopSwIJ08G1P." } UserMock .expects('findOne').withArgs({ email: 'test@email.com' }) .chain('exec') .resolves(expectedResult) User.findByEmail('test@email.com') .then((user) => user.comparePassword('password')) .then((user) => user.publishParse(user)) .then((user) => { UserMock.verify() UserMock.restore() assert.equal(user, {expectedResult.email, id: expectedResult._id}) done() }) .then(console.log) .catch(console.log) •型号: … […]

在节点中testing失败的请求

我有一些如下所示的代码: var request = require('request'); function Service(){ this._config = require('../path/to/config.json'); } Service.prototype.doThing = function(){ return new Promise(function(resolve, reject){ request.post(url, {payload}, function(error, response, body){ //handle response resolve(true); }).on('error', function(err){ //handle errors resolve(false); }); }); } 我试图testing运行错误的块,但由于承诺而有困难。 我正在使用摩卡进行testing运行,并使用了sinon进行testing。 我能够存根请求,以便我可以计算on方法被调用的次数,但是包含的Promise永远不会parsing。 有一些软件包可以和sinon一起处理promise(我已经尝试过sinon-promise和sinon-stub-promise),但是我必须存储整个doThing方法才能正确parsing。 我将不胜感激在任何方式来testing这个代码或替代的代码结构,可能更容易testing的input。 有问题的testing(等待doThing承诺返回)如下: context('when the server is unavailable', function(){ beforeEach(function() { var onStub = sinon.stub(); requestStub = {post: […]

sinon:如何模拟从另一个函数返回的函数

我正在尝试使用simple-git 。 我需要编写一些unit testing,为此我需要使用sinon来模拟一些函数。 我遇到的问题是,我的嘲笑不传播到我的testing文件。 例如,在testing的文件中,我有这样的: const git = require('simple-git/promise') function func () { var promise if (repo_exists()) { promise = git().silent(true).clone('http://github.com/me/my-repo.git') } else { promise = git('my-repo').silent(true).pull('origin','master') } promise.then(() => { // do more stuff }) } 在我的testing文件中,我试过这个: const git = require('simple-git/promise')() sinon.stub(git, 'silent').callsFake(() => { return { clone: () => { console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~') console.log('calling […]