lundi 26 octobre 2020

how to mock a method using callArgsWith or yields

How do you mock an outer method calling a callback using sinon? Example given the following code, createTable should return 'a string' as response in the callback function.

 const db= new AWS.DynamoDB();
 db.createTable(params, (err, data) => {

  if (err) {
    console.error('Unable to create table. Error JSON:');
    if (err.code !== 'ResourceInUseException') reject();
    else {
      console.log('Table already exists, proceeding.');
      resolve();
      return;
    }
  } else {
    console.log('Created table. Table description JSON:');
  }
  console.log('Waiting for DDB Table to get created');
});

Below is sample unit test that I've worked

beforeEach(()) => {
  DynamoDBMock = class DynamoDB { 
  constructor() {
    this.createTable = () => ({
      promise: createTableStub,
    });
  
  }
};
});

   it('should return', done => {
     createTableStubPromise.callsArgWith(1, null, 'test');
     createTableStubPromise.callsArgWith(1, null, 'test');
});

Aucun commentaire:

Enregistrer un commentaire