Tag: sinon

nodejs mssql并承诺混淆

我是非常新的承诺在JavaScript中,所以这个问题是帮助我弄清楚为什么我得到一个错误(以奇怪的顺序)使用承诺。 最重要的是,我正在忙于使用ms-sql回购,sinonjs和第一次restify,所以也没有帮助 这个问题和我之前问过的有关 在上面的问题中,我需要在@robertklep的帮助下完成我成功完成的SQL DB。 然而,作为一个完整的检查,我想检查终点是否仍然像以前那样返回我期望的数据,所以我解开了残局。 现在我收到以下错误,我不知道为什么: [错误:发送后无法设置标题。] testing: 'use strict'; var expect = require('chai').expect, request = require('supertest'), chance = require('chance').Chance(), server = require('../server'), sinon = require('sinon'), select = require('../../helpers/data_access/select'), read_file = require('../../helpers/read_file'); describe("/account_types", function () { // before(function (done) { // sinon // .stub(select, "query_list") // .returns([{id: "test"}]); // // sinon // .stub(select, "query_single") […]

存根nodejs承诺在链中返回错误

我nodejs使用nodejs中的nodejs ,也是在testing它们。 我已经单独testing了各个模块,但是当涉及testingchain of promises ,我遇到了一些麻烦。 我尝试了下面的例子,在npm页面上find了sinon-as-promised的例子,但似乎没有设法控制stream程并触发链中第一个promise的错误。 我正在使用mocha , chai和sinon for作为我sinon的testing, sinon-as-promised和chai-as-promised 。 我试图testing这个模块: 'use strict'; var mySQS = require('./modules/sqs/sqs-manager'); var sWebHook = require('./modules/webhooks/shopify/webhooks'); var main = {}; main.manageShopifyWebhook = function (params, callback) { sWebHook.verify(params.srcHmac, params.rawBody, params.shopName.split('.myshopify.com')[0], params.productId) .then(function(data) { var body = { "params": { "productId": data.productId, "shopName": data.shopName }, "job": "call-update-item" }; mySQS.create_Queue(body) […]

间谍的解构要求的方法

我有一个方法,我input一个解构方法。 我试图窥探这个方法,但仍然遇到错误。 const { addition } = require('./my-math-lib'); const additionSpy = sinon.spy(addition); testMethod.doMath(); expect(additionSpy.calledOnce).to.be.equal(true); 不幸的是,这个unit testing不起作用。 乳清我看着additionSpy对象,我看到notCalled是真的。 如果我将代码更改为 – const mathStuff = require('./my-math-lib'); const additionSpy = sinon.spy(mathStuff, 'addition'); testMethod.doMath(); expect(additionSpy.calledOnce).to.be.equal(true); 这个unit testing会通过。

节点Stub一个返回对象的方法

我有一个模块,有一些属性。 我正在使用它如下 Var propmodule = require('me-props'); var prop = new propmodule('server'); prop.get('min); //returns 3 prop.get('max') //returns 10 我必须嘲笑这个testing。 下面的代码使用了proxyquire和sinon var spro = proxyquire('../lib/add.js',{ 'me-props' : sinon.stub.returns({ get : sinon.stub.returns({ min :'3', max : '10 )} )} }) 上面的代码工作。 但在testing时,get方法调用返回一个对象。 get(min)返回一个对象。 var a = prop.get()然后a('min')返回3.但prop.get('min')返回一个对象。 如何修改存根以返回获取调用的值?

与摩卡手表一起使用时,Sinon存根有奇怪的performance

所以这里是我们的基本设置 src/MyComponent.js import actions from `src/actions` export const mapDispatchToProps = dispatch => { return { onClick: () => { dispatch(actions.myAction()) } } } src/actions.js const actions = { myAction: () => () => … } export default actions src/MyComponentTest.js import sinon from 'sinon' import actions from 'src/actions' import { mapDispatchToProps } from 'src/MyComponent' describe('onClickTests, () […]

Sinon stubbing函数作为parameter passing

我有以下的示例类: function Example() {…} Example.prototype.someFunc1() {…} Example.prototype.someFunc2() {…} Example.prototype.func(func) {var res = func(); …} 我通常调用Example#func() ,如下所示: var example = new Example(); example.func(example.someFunc1) // or like this, depending on what I want example.func(example.someFunc2) 现在我在我的testing中存根Example#someFunc1()如下: var example = new Example(); sinon.stub(example, 'someFunc1').returns(…); exmaple.func(example.someFunc1); 问题是Example#someFunc1()没有被这种方式存根并被正常调用。 在这种情况下我能做些什么?

推迟与巴贝尔import

我正在一个基于ReactJS的大型网站上工作,需要编写一些testing,我可以推迟加载各种模块,因为我需要先build立Sinon。 基本设置看起来像这样 const assert = require('chai').assert; const sinon = require('sinon'); const reducerUtils = require('../../app/utils/reducerUtils'); const connectToReducerStub = sinon.stub( reducerUtils, 'connectToReducer', (stateMap, actionMap) => { console.log(`Connecting to reducer`); } ); 在这一点上,我只需要导入一个组件,试图使用reducerUtils.connectToReducer我已经reducerUtils.connectToReducer ,但我的问题是,如果我喜欢在其他地方的代码库: import MyComponent from '../../app/components/myComponent.jsx'; 导入是作为第一件事(在所有其他事情之前)运行,使得导入和调用reducerUtils.connectToReducer发生之前,我把它存根。 另一方面,试图通过使用require来推迟它似乎不起作用: const MyComponent = require('../../app/components/myComponent.jsx'); 这里发生的是MyComponent只是undefined 。 我正在使用ES6类来定义我的组件: import react from 'react'; import connectToReducer from '../../utils/reducerUtils'; class MyComponent { […]

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

我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嘲笑Sequelize

我有以下使用Sequelize的nodejs函数: var processDatabase = function (dbConnection, schema, recordsets) { var myLogTable = dbConnection.define(schema.tableName, schema.myLogSchema, schema.myLogSchemaIndex); myLogTable.sync({ force: false, freezeTableName: true, logging: console.log }).then(function () { console.log('Table synced…'); for (k = 0; k < recordsets.length; k++) { var query = "Some query"; dbConnection.query( query, { type: dbConnection.QueryTypes.SELECT } ) .then(function (results) { console.log('MYSQL Selection Done'); }) […]

testing总是与诗乃和柴一起传递

我正在使用Mocha,Chai和Sinon来testing一些Node方法。 这个testing通过,当我把'calledOnce'改为'calledTwice'时,它会按预期的那样失败。 it('should call checkIfRoomExists once', function (done) { var check = sandbox.spy(RoomInfoModel, 'checkIfRoomExists'); ViewBusiness.getViewToRender("thisisanoneknownroom", function (viewName) { expect(check.calledOnce).to.equal(true); done(); }) }); 但是,当我尝试并按照教程“设定”是这样设置的: it('should call checkIfRoomExists once', function (done) { var check = sandbox.spy(RoomInfoModel, 'checkIfRoomExists'); ViewBusiness.getViewToRender("thisisanoneknownroom", function (viewName) { expect(check).to.have.been.calledTwice; done(); }) }); 请注意,我在第二个testing中正在testing“calledTwice”。 它仍然通过。 如果我将它更改为“notCalled”,它仍然通过。 基本上它总是通过。 我错过了什么?