sinon

Using Sinon to stub chained Mongoose calls

大憨熊 提交于 2019-12-30 04:12:12
问题 I get how to stub Mongoose models (thanks to Stubbing a Mongoose model with Sinon), but I don't quite understand how to stub calls like: myModel.findOne({"id": someId}) .where("someBooleanProperty").equals(true) ... .exec(someCallback); I tried the following: var findOneStub = sinon.stub(mongoose.Model, "findOne"); sinon.stub(findOneStub, "exec").yields(someFakeParameter); to no avail, any suggestions? 回答1: I've solved it by doing the following: var mockFindOne = { where: function () { return

How to stub SOAP client request with sinon in Node JS?

左心房为你撑大大i 提交于 2019-12-25 17:03:26
问题 I am using strong-soap module to get data from SOAP request. var soap = require('strong-soap').soap; soap.createClient(url, options, function (err, client) { var method = client.GetInfoSOAP; method(requestQuery, function (err, info) { // bla bla } } I am getting the required data. Now I want to write unit test case to mock the SOAP request using sinon stub, but didn't get any success. Any help would be appreciated. 回答1: What you want is controlling the soap object's createClient . You can do

How to stub SOAP client request with sinon in Node JS?

我的梦境 提交于 2019-12-25 17:03:15
问题 I am using strong-soap module to get data from SOAP request. var soap = require('strong-soap').soap; soap.createClient(url, options, function (err, client) { var method = client.GetInfoSOAP; method(requestQuery, function (err, info) { // bla bla } } I am getting the required data. Now I want to write unit test case to mock the SOAP request using sinon stub, but didn't get any success. Any help would be appreciated. 回答1: What you want is controlling the soap object's createClient . You can do

sinon spy on function not working

家住魔仙堡 提交于 2019-12-24 20:56:12
问题 I'm trying to write a standalone test for this simple middleware function function onlyInternal (req, res, next) { if (!ReqHelpers.isInternal(req)) { return res.status(HttpStatus.FORBIDDEN).send() } next() } // Expose the middleware functions module.exports = { onlyInternal } This does not work describe('success', () => { let req = { get: () => {return 'x-ciitizen-token'} } let res = { status: () => { return { send: () => {} } } } function next() {} let spy before(() => { spy = sinon.spy(next

Unit Test a Node.js application with Mocha, Chai, and Sinon

*爱你&永不变心* 提交于 2019-12-24 07:59:22
问题 I am new to unit testing Node.js application. My application converts CSV file to JSON after some filtering. var fs = require('fs'); var readline = require('readline'); module.exports = ((year) => { if (typeof year !== "number" || isNaN(year)){ throw new Error("Not a number"); } var rlEmitter = readline.createInterface({ input: fs.createReadStream('./datasmall.csv'), output: fs.createWriteStream('./data.json') }); rlEmitter.on('line', function(line) { /*Filter CSV line by line*/ }); rlEmitter

Spying on Date constructor with sinon

狂风中的少年 提交于 2019-12-24 04:29:29
问题 I have a method that sets expiration date of a token: var jwt = require('jwt-simple'); module.exports = { setExpirationDate: function(numDays) { var dateObj = new Date(); console.log(dateObj); } } I want to write an assert on "new Date" statement: var jwtHelper = require('../../../helpers/jwtToken'); describe('setExpirationDate method', function() { it('should create date object', function() { var Date = sinon.spy(Date); jwtHelper.setExpirationDate(global.TOKEN_EXPIRE_DAYS); expect(Date).to

How to stub constant functions when using ES Modules with sinon.js?

戏子无情 提交于 2019-12-24 01:39:32
问题 With ES Modules, we define some exported functions as const and it is often a standard practice. However, during unit testing, these const functions can't be stubbed out which seems problematic from a unit testing perspective. For example, import * as fs from 'fs'; import { bindNodeCallback } from 'rxjs'; import { map } from 'rxjs/operators'; // readFile :: string, string => Observable<string> export const readFile$ = bindNodeCallback(fs.readFile); // readJson :: string => Observable<any>

How can I stub methods attached to the 'this' object using Sinon.js?

旧街凉风 提交于 2019-12-24 01:14:27
问题 I'm attempting to test the 'onInit' function attached below: function (jQuery, Controller, JSONModel) { "use strict"; var controls; var mainController = Controller.extend("tool.controller.Main", { onInit: function(oEvent) { var inputModel = new JSONModel("./model/inputs.json"); var productsModel = new JSONModel("./model/products.json"); this.getView().setModel(inputModel, "inputModel"); this.getView().setModel(productsModel); controls = viewControls.main.apply(this); }, ... Objective For this

Pulling in sinon submodules in browser with AMD and webpack

假装没事ソ 提交于 2019-12-24 00:38:41
问题 I'm running into the same problem described in this question. Basically sinon only pulls in all required submodules if you're using node. If you try to use it in the browser with AMD (I'm using webpack) apparently you have to manually include lib/spy.js . I have a webpack module that currently exports sinon: exports.sinon = require('sinon'); How would I modify that to roll in spy.js ? I think this is more of a CommonJS syntax question than anything. 回答1: The following seems to work: exports

Sinon.spy on a method is not invoked

試著忘記壹切 提交于 2019-12-24 00:33:15
问题 The code I'm testing is fairly simple: it invokes a method in case a condition is verified. If not, it invokes another method contained within the first one as an attribute. app.js: function test (fn, isActivated) { if (isActivated) { return fn('foo') } return fn.subFn('bar') } var fn = function (p) { return p } fn.subFn = function (p) { return 'sub-' + p } var resFn = test(fn, true) var resSubFn = test(fn, false) document.write(resFn) // shows 'foo' as expected document.write(resSubFn) //