Tag: sinon chai

Sinon称为With和calledWithMatch失败的对象编辑:Sinon间谍对象的构造函数

我是unit testing新手,正在使用Mocha,Sinon和Chai来testingNodeJs代码。 问题是我对stub.calledWith()的期望总是失败,即使testing错误显示了两个在语法上相同的对象。 我使用Sinon来存储一个mongoose模型的保存方法,并检查这个存根被调用了正确的细节。 我正在testing的function: async function createGroup(details) { 'use strict'; Logger.info(`create group called`); const group = new UserGroup(this.formatGroup(details)); const result = await group.save(); Logger.verbose(`results of save: ${JSON.stringify(result)}`); return result; } unit testing describe('object creation', function () { 'use strict'; const saveStub = sinon.stub(UserGroup.prototype, 'save'); mongoose.Promise = Promise; beforeEach(function (done) { saveStub.reset(); return done(); }); […]

使用柴和冼方法在一个方法内扼杀诺言

我testing的function大致是这样的; function doThing(data, callback) { externalService.post('send').request(data) .then(() => { if (callback) { callback(); } }) .catch((message) => { logger.warn('warning message'); if (callback) { callback(); } }); } 我试图用柴和诗乃来testing。 我尝试过不同的指南,我目前的咒语看起来像; const thingBeingTested = require('thing-being-tested'); const chai = require('chai'); const sinon = require('sinon'); require('sinon-as-promised'); const sinonChai = require('sinon-chai'); const expect = chai.expect; var chaiAsPromised = require('chai-as-promised'); chai.use(chaiAsPromised); […]

使用sinon和mocha来保存pg-promise

假设我有一个以下模块,作为database.js const initOptions = {} const pgp = require('pg-promise')(initOptions) const config = require('../../config') const db = pgp({ host: config.database.host, port: config.database.port, database: config.database.database, user: config.database.user, password: config.database.password }) module.exports = db 和下面的模块一样create.js const db = require('./database') function create (name) { return new Promise((resolve, reject) => { db.func('create', name) .then(data => { return resolve(data) }) .catch(err […]

Stubbing Fetch Call – response.json不会调用

我试图用sinon和sinon-stub-promise一个获取呼叫。 我非常接近…但我不知道为什么它不会调用我创build的response.json()方法。 function toJSON(response) { console.log(response); return response.json(); } function init() { fetch(darkWeatherUrl) .then(toJSON) .then(computeHours) .then(sendAlerts) .catch((e) => { console.log('init error ' + e); }); } describe('lifx alert test', ()=> { it('should run fetch', ()=> { var fetch = sinon.stub().returnsPromise(); var body = { "hourly": { data: [ { time: 1493413200, icon: 'clear-day', precipIntensity: 0, […]

我如何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其他? 谢谢。