Search code examples
unit-testingsinonsinon-chai

How to assert sinon .stub called


So I have the following AWS call I am mocking out

    const db = new aws.DynamoDB.DocumentClient();
    await db.get({
           TableName: myTable,
           Key: { 'id': id }
          }).promise();

with the mock being:

        MockAws = {
        DynamoDB: {
            DocumentClient: class {
                get = sinon.stub().returns({promise: async () => (awsDynamoRespose)})
                }
            }
        }

    lambda = proxyquire('../src/handler', {
        'aws-sdk' : MockAws
    });

So this works, I see it returning my object, and the code can be tested. However, since the code under test doesn't return anything, I have to assert by the mocked calls, which when I try to assert, get is undefined.

   console.log(MockAws.DynamoDB.DocumentClient.get)  //returns undefined
   sinon.assert.calledOnce(MockAws.DynamoDB.DocumentClient.get)  //returns fake is not a spy

I have to be close here. How can the method be returning an object, but yet then be undefined? DocumentClient does exist.

Thanks


Solution

  • Continue my streak of finding an answering only after posting....

    let awsMockGet
    
    //inside before each
    awsMockGet =  sinon.stub().returns({promise: async () => (awsDynamoRespose)})
    
            MockAws = {
                DynamoDB: {
                    DocumentClient: class {
                        get = awsMockGet
                        }
                    }
                }
    

    in test

    sinon.assert.calledOnce(awsMockGet)