sinon

unit testing express route with async callback

被刻印的时光 ゝ 提交于 2019-12-23 20:17:03
问题 I'm writing an app in node.js using express. I seperated the route definition from express so I will be able to unit test it without mocking out express. What I did is: file: usersRoutes.js var routes = { getAll: function(req,res) { var db = req.db; // req.db is initialized through a dedicated middleware function var usersCollection = db.get('users'); usersCollection.find({},{limit:10},function(e,results){ res.send(results); }); } }; module.exports = routes; file: users.js var express =

Sinon stubbing helper method defined in same file

僤鯓⒐⒋嵵緔 提交于 2019-12-23 09:52:37
问题 So I have a file, user-database , that looks something like this : export function foo(id: number): Promise<boolean> { return new Promise<boolean>((resolve, reject) => { findSomething(id) .then((data) => { //do something with data }) } } export function findSomething(id: number): Promise<Object> { return new Promise<Object> ((resolve, reject) => { let query = 'SELECT * FROM user'; db.executeQuery(query); .then(data) => { if(data.length < 1) { reject(new Error('whoops')); } resolve(data); },

sinon stub not replacing function

我是研究僧i 提交于 2019-12-23 09:31:31
问题 I'm trying to use sinon stub to replace a function that might take along time. But when I run the tests, the test code doesn't seem to be using the sinon stubs. Here is the code I'm trying to test. function takeTooLong() { return returnSomething(); } function returnSomething() { return new Promise((resolve) => { setTimeout(() => { resolve('ok') }, 1500) }) } module.exports = { takeTooLong, returnSomething } and this is the test code. const chai = require('chai') chai.use(require('chai-string'

Testing JavaScript Click Event with Sinon

心不动则不痛 提交于 2019-12-23 07:46:44
问题 I am trying to produce some test to be able to better understand how to test DOM events with the combination of Mocha, Chai, Sinon and jQuery. I want to check that the alert function is correctly triggered on a click of the div element. I know that the setup of the HTML element is correct jQuery, but I'm not entirely sure how to produce a passing test for the code below. What's particularly strange is that I get a dialogue appearing on opening the HTML file in my browser, so I know the line '

sinon - spy on toString method

六眼飞鱼酱① 提交于 2019-12-23 05:20:02
问题 In my file, I have something like this: if(somevar.toString().length == 2) .... How can I spy on toString from my test file? I know how to spy on things like parseInt with: let spy = sinon.spy(global, 'parseInt') But that doesn't work with toString since it's called on the value, I tried spying on Object and Object.prototype , but that doesn't work either. 回答1: You can't call sinon.spy or sinon.stub on a method of primitive value like: sinon.spy(1, 'toString') . This is wrong. You should call

How do I use Sinon to stub interaction with a database using mssql library?

走远了吗. 提交于 2019-12-23 02:41:38
问题 Using the node-mssql library to pull data from SQL. I've been using Sinon for a while now (written about 200 tests with it); having a ton of trouble getting my head around how to stub this library out. The code looks like: var sql = require('mssql'); var conn = new sql.Connection(sqlConfig); // sqlConfig is connection info, defined elsewhere conn.connect(function(err) { var req, selectFromTable; if (err != null) { // handle error } req = new sql.Request(conn); selectFromTable = "select * from

Testing Restify Route Handler that contains Promise Code Block, using SinonJs and Mocha

我的未来我决定 提交于 2019-12-23 00:48:08
问题 I have a restify action code block below: function retriveAll(req, res, next) { db.user .find({where: {id: 1}) .then(function(user){ res.send(user); }) .catch(function(details){ res.send(details.message); }) .finally(function(){ next(); }); } I want to test this action specifically validating that res.send() was called within this code block . And later on validating the res.send() returned data. I'm using SinonJs and Mocha for testing framework. Here's a sample test code block for the method

Sinon/Mocha test with async ajax calls didn't return promises

女生的网名这么多〃 提交于 2019-12-22 11:27:38
问题 I'm writing some test for my client side api use karma with Mocha and Sino . But I'm stuck on getting the async process. import api from '../../../src/api'; import stubData from '../data'; import axios from 'axios'; /* eslint-disable prefer-arrow-callback,func-names */ describe('API test', function () { before(function () { this.server = sinon.fakeServer.create(); }); after(function () { this.server.restore(); }); it('Should return cart with provided token', function (done) { this.server

How to unit test localStorage using sinon

时光毁灭记忆、已成空白 提交于 2019-12-22 09:48:45
问题 I am trying to test localStorage using sinon. Basically I am very new to unit testing so this might be very basic. Update I managed to come up with this but now its giving me a new error Should wrap property of object Test describe('Initial State', () => { it('should set the initial state for the component', () => { const props = { currentUser: {} }; sinon.stub(window.localStorage, 'setItem'); window.localStorage.setItem('none', 'nothing'); }); }); 回答1: I managed to resolve it. Thanks to

sails.js + mocha + supertest + sinon: how to stub sails.js controller function

痞子三分冷 提交于 2019-12-22 08:35:08
问题 I am trying to stub a sails controller function, but I don't know which object to stub. using sinon.stub(object,'funcname', function()... This is probably related to the way sails bind controller functions... Here is some code to give example Controller file api/controllers/PersonController.js var fs = require('fs'); // // I want to stub retrieveData function when testing // function retreiveData(cb) { fs.readFile('./filedata', function (err, data) { if (err) throw err; cb(data.toString()); }