4

I need to assert whether a constructor was called using sinon. Below is how I can create a spy.

let nodeStub: any;
nodeStub = this.createStubInstance("node");

But how can I verify that this constructor was called with the relevant parameters? Below is how the constructor is actually called.

 node = new node("test",2);

Any help would be much appreciated.

Below is the code I have.

import {Node} from 'node-sdk-js-browser';

export class MessageBroker {

    private node: Node;
    constructor(url: string, connectionParams: IConnectionParams) {
        this.node = new Node(url, this.mqttOptions, this.messageReceivedCallBack);
    }
}
AnOldSoul
  • 4,017
  • 12
  • 57
  • 118

2 Answers2

1

Given the following code myClient.js:

const Foo = require('Foo');

module.exports = {
   doIt: () => {
      const f = new Foo("bar");
      f.doSomething();
  }
}

You can write a test like this:

const sandbox = sinon.sandbox.create();
const fooStub = {
   doSomething: sandbox.spy(),
}

const constructorStub = sandbox.stub().returns(fooStub);
const FooInitializer = sandbox.stub({}, 'constructor').callsFake(constructorStub);

// I use proxyquire to mock dependencies. Substitute in whatever you use here.
const client2 = proxyquire('./myClient', {
    'Foo': FooInitializer,
});

client2.doIt();

assert(constructorStub.calledWith("bar"));
assert(fooStub.doSomething.called);     
steve
  • 954
  • 1
  • 10
  • 18
0
//module.js
var Node = require('node-sdk-js-browser').Node;

function MessageBroker(url) {
  this.node = new Node(url, 'foo', 'bar');
}

module.exports.MessageBroker = MessageBroker;

//module.test.js
var sinon = require('sinon');
var rewire = require('rewire');
var myModule = require('./module');
var MessageBroker = myModule.MessageBroker;

require('should');
require('should-sinon');

describe('module.test', function () {
  describe('MessageBroker', function () {
    it('constructor', function () {
      var spy = sinon.spy();
      var nodeSdkMock = {
        Node: spy
      };
      var revert = myModule.__set__('node-sdk-js-browser', nodeSdkMock);

      new MessageBroker('url');

      spy.should.be.calledWith('url', 'foo', 'bar');
      revert();
    });
  });
});
Alexey Kucherenko
  • 885
  • 1
  • 8
  • 11
  • hey mate, my class that needs to be tested is called MessageBroker. It has this import line , "import {Node} from 'node-sdk-js-browser'; ". Here Node is a named import. And its coming from the module node-sdk-js-browser. The Node constructor is what I need to spy and verify whether it was called. Can you edit that example to suit this? Since I'm new to Javascript its pretty confusing :( – AnOldSoul Jul 09 '16 at 11:17
  • It should provide you the general idea. if you provide me the code (even simplified) I can update my example. – Alexey Kucherenko Jul 09 '16 at 14:30
  • I have edited the question with my code mate. I'm writing tests for MEssageBroker class. I need to verify that the Node constructor was called when I call the MessageBroker constructor. Would be great if you can help me with an example for this :( – AnOldSoul Jul 09 '16 at 15:16
  • updated my post but wrote it using es5 instead of es6 – Alexey Kucherenko Jul 11 '16 at 18:02