Question

How do you mock or stub class instantiation with SinonJS? I want to make assertions on the parameters that are passed to the constructor.

var myClass = new MyClass({ params: "To Test" } ); // how can I mock the 'new' on MyClass?
Was it helpful?

Solution

What do you do in the constructor? Do you assign the parameters to the MyClass object? If so,

var params = {param: 'To Test'};
myClass = new MyClass(params);

Then check that myClass.param = params.param.

Calling var myClass = new MyClass(params) is the same as calling

var myClass = {};
MyClass.call(myClass, params);
myClass.__proto__ = MyClass.prototype; //This is considered bad practice, but is fine for testing.

Is this what you're asking? If not, what kinds of assertions on parameters are you hoping to make?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top