Dynamoose: How can you mock the database calls?

Created on 7 Dec 2017  路  9Comments  路  Source: dynamoose/dynamoose

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?

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.

All 9 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mcalhoun picture mcalhoun  路  6Comments

androidand picture androidand  路  3Comments

MickL picture MickL  路  7Comments

Mojo90 picture Mojo90  路  10Comments

arifsetyawan picture arifsetyawan  路  8Comments