简体   繁体   中英

How do you mock/stub class instantiation with SinonJS?

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?

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM