Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
145
rated 0 times [  146] [ 1]  / answers: 1 / hits: 30949  / 9 Years ago, sun, september 13, 2015, 12:00:00

I'd like to unit test the following ES6 class:



// service.js
const InternalService = require('internal-service');

class Service {
constructor(args) {
this.internalService = new InternalService(args);
}

getData(args) {
let events = this.internalService.getEvents(args);
let data = getDataFromEvents(events);
return data;
}
}

function getDataFromEvents(events) {...}

module.exports = Service;


How do I mock constructor with Sinon.JS in order to mock getEvents of internalService to test getData?



I looked at Javascript: Mocking Constructor using Sinon but wasn't able to extract a solution.



// test.js
const chai = require('chai');
const sinon = require('sinon');
const should = chai.should();

let Service = require('service');

describe('Service', function() {
it('getData', function() {
// throws: TypeError: Attempted to wrap undefined property Service as function
sinon.stub(Service, 'Service').returns(0);
});
});

More From » node.js

 Answers
8

You can either create a namespace or create a stub instance using sinon.createStubInstance (this will not invoke the constructor).



Creating a namespace:



const namespace = {
Service: require('./service')
};

describe('Service', function() {
it('getData', function() {
sinon.stub(namespace, 'Service').returns(0);
console.log(new namespace.Service()); // Service {}
});
});


Creating a stub instance:



let Service = require('./service');

describe('Service', function() {
it('getData', function() {
let stub = sinon.createStubInstance(Service);
console.log(stub); // Service {}
});
});

[#65084] Thursday, September 10, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sageallenv

Total Points: 458
Total Questions: 102
Total Answers: 104

Location: Venezuela
Member since Thu, Jul 15, 2021
3 Years ago
;