How to use the createStubInstance function from sinon

Find comprehensive JavaScript sinon.createStubInstance code examples handpicked from public code repositorys.

sinon.createStubInstance is a method in the Sinon library that creates a new object with all of its methods replaced with stub functions.

113
114
115
116
117
118
119
120
121
122
123
 * except the EventEmitter functionality, which works as expected.
 * @param BaseClass - The base class to stub.
 * @returns A stubbed instance with EventEmitter mixed in.
 */
function createEmitterStub(BaseClass) {
  const stub = sinon.createStubInstance(BaseClass);


  Object.getOwnPropertyNames(EventEmitter.prototype).forEach(name => {
    const property = EventEmitter.prototype[name];
    if (typeof property !== 'function') { return; }
fork icon27
star icon20
watch icon20

+ 2 other calls in file

20
21
22
23
24
25
26
27
28
29
30
31
describe('Asset Transfer Basic Tests', () => {
    let transactionContext, chaincodeStub, asset;
    beforeEach(() => {
        transactionContext = new Context();


        chaincodeStub = sinon.createStubInstance(ChaincodeStub);
        transactionContext.setChaincodeStub(chaincodeStub);


        chaincodeStub.putState.callsFake((key, value) => {
            if (!chaincodeStub.states) {
fork icon0
star icon2
watch icon1

+ 3 other calls in file

How does sinon.createStubInstance work?

sinon.createStubInstance is a method in the Sinon library that takes a constructor function as its argument and returns a new object that has all of its methods replaced with stub functions. This can be useful in test scenarios where you need to replace a complex object with a simpler one that behaves in a predictable way. When called, sinon.createStubInstance does the following: Creates a new object using the constructor function passed as an argument. Replaces each method on the object with a stub function using sinon.stub. Returns the new object with its methods stubbed out. Note that sinon.createStubInstance does not execute the constructor function, and therefore does not create any properties on the object that may be initialized by the constructor. It only creates an object with stubbed methods. Overall, sinon.createStubInstance provides an easy way to create objects with simple, predictable behavior for testing purposes.

26
27
28
29
30
31
32
33
34
35

chaincodeStub = sinon.createStubInstance(ChaincodeStub);
chaincodeStub.getMspID.returns('org1');
transactionContext.setChaincodeStub(chaincodeStub);

clientIdentity = sinon.createStubInstance(ClientIdentity);
clientIdentity.getMSPID.returns('org1');
transactionContext.clientIdentity = clientIdentity;

chaincodeStub.putState.callsFake((key, value) => {
fork icon0
star icon0
watch icon1

166
167
168
169
170
171
172
173
174
175
let stubWalletFacadeFactory;
let stubInMemoryAndFileSystemWalletFacade;

beforeEach(() => {
    stubWalletFacadeFactory = sinon.createStubInstance(IWalletFacadeFactory);
    stubInMemoryAndFileSystemWalletFacade = sinon.createStubInstance(IWalletFacade);
    stubWalletFacadeFactory.create.resolves(stubInMemoryAndFileSystemWalletFacade);
});

describe('When being created by it\'s factory', () => {
fork icon0
star icon0
watch icon1

+ 17 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const sinon = require("sinon");

class ExampleClass {
  method1() {}
  method2() {}
  method3() {}
}

const stub = sinon.createStubInstance(ExampleClass);

// Invoke stubbed methods on the stub object
stub.method1();
stub.method2();
stub.method3();

// Check if methods were called
sinon.assert.calledOnce(stub.method1);
sinon.assert.calledOnce(stub.method2);
sinon.assert.calledOnce(stub.method3);

In this example, we create a class ExampleClass with three methods. We then create a stubbed instance of this class using sinon.createStubInstance. The resulting stub object has all of its methods replaced with stub functions. We can then invoke the stubbed methods on the stub object as if they were actual methods of ExampleClass. In this case, we call method1, method2, and method3. Finally, we use Sinon's sinon.assert functions to check that each method was called exactly once. By using sinon.createStubInstance, we can easily create stubbed instances of classes for use in unit tests, making it easier to test code that depends on complex objects.