Javascript: Mocking Constructor using Sinon

本小妞迷上赌 提交于 2019-12-04 22:13:32

I needed a solution for this because my code was calling the new operator. I wanted to mock the object that the new call created.

var MockExample = sinon.stub();
MockExample.prototype.test = sinon.stub().returns("42");
var example = new MockExample();
console.log("example: " + example.test()); // outputs 42

Then I used rewire to inject it into the code that I was testing

rewiredModule = rewire('/path/to/module.js');
rewiredModule.__set__("Example", example);
ppoliani

From the official site of sinonjs:

Replaces object.method with a stub function. The original function can be restored bycalling object.method.restore(); (or stub.restore();). An exception is thrown if the property is not >already a function, to help avoid typos when stubbing methods.

this simply states that the function for which you want to create the stub must be member of the object object.

To make things clear; you call

sinon.stub(window, "MyWidget");

The MyWidget function needs to be within the global scope (since you pass window as parameter). However, as you already said, this function is in a local scope (probably defined within an object literal or a namespace).

In javascript everyone can have access to the global scope, but not the other way around.

Check where you declare the MyWidget function and pass container object as first parameter to sinon.stub()

Using Sinon 4.4.2, I was able to mock an instance method like this:

sinon.stub(MyClass.prototype, myMethod).resolves(tesObj)

I needed a mock for a line that looked something like:

let someData = await new MyClass(token).myMethod(arg1, arg2)

A similar solution provided here: Stubbing a class method with Sinon.js

I used Mockery to Mock a Constructor/Function without any problems.

var mockery = require('mockery');
var sinon = require('sinon');

mockery.enable({
  useCleanCache: true,
  warnOnReplace: false,
  warnOnUnregistered: false
});

exports.Client = function() {/* Client constructor Mock */};
var ClientSpy = sinon.spy(exports, 'Client');
mockery.registerMock('Client', ClientSpy);

var Factory = require('Factory'); // this module requires the Client module

You should be able to apply a Sinon Spy just as the example above does.

Make sure to disable or reset Mockery after the test(s)!

Just found this in the documentation.

If you want to create a stub object of MyConstructor, but don’t want the constructor to be invoked, use this utility function.

var stub = sinon.createStubInstance(MyConstructor)

I ran into this error by mistakenly typing sinon.stub.throws(expectedErr) rather than sinon.stub().throws(expectedErr). I've made similar mistakes before and not encountered this particular message before, so it threw me.

Use sinon.createStubInstance(MyES6ClassName), then when MyES6ClassName is called with a new keyword, a stub of MyES6ClassName instance will returned.

I know the original question is a bit old by now, but, did you try to stub the prototype of the class?

const myStub = sinon.stub(MyClass.prototype, 'myMethodReturnsFalse').returns(true)
const myInstance = new MyClass()
expect(myInstance.myMethodReturnsFalse()).equal(true)

I was able to get StubModule to work after a few tweaks, most notably passing in async:false as part of the config when requiring in the stubbed module.

Kudos to Mr. Davis for putting that together

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!