Is it possible to mock the database calls with dynamoose?
I have looked into using https://www.npmjs.com/package/aws-sdk-mock but without much joy.. is there a preferred method to mock?
What is your main goal? You can use a local mock database the test: https://dynamoosejs.com/api#dynamooselocalurl
Closing due to no reply. Please let us know if you are still having this issue and we can try to help further.
So how does one go about mocking database calls from Dynamoose? I am trying to unit test a function that creates a user with the Model.create() function. How would I go about mocking that and returning a successful response so that my unit test passes?
@Br0kensword One (not ideal) way to do testing is to use a local DynamoDB instance, which you can populate with a client like dynamon. However, I am myself curious about how to do actual request mocking.
@brandongoode Using a local db is no longer really a unit test, it's more of an integration test. You also run the risk of setup data from one test effecting the results of other tests. I know it's possible for that to happen anyway, but I would think the likelihood is greater when using a local database.
@ckuiawa is right.
Sinon.js does the trick for me. Only just got it working but looking good.
const sandbox = sinon.createSandbox();
sandbox
.stub(dynamoose, 'model')
.returns({
update: (args) => { ... }
})
@jacktuck can you share full example if possible
@ehtesham1996 Sorry I don't have an example to hand. I'd maybe do something similar to how dynamoose mocks itself here https://github.com/dynamoose/dynamoose/blob/master/test/unit/Model.js#L79
@ehtesham1996 This project I created, along with dynamoose.aws.ddb.set and dynamoose.aws.ddb.revert should do the trick.
Most helpful comment
@brandongoode Using a local db is no longer really a unit test, it's more of an integration test. You also run the risk of setup data from one test effecting the results of other tests. I know it's possible for that to happen anyway, but I would think the likelihood is greater when using a local database.