Mocking database in node.js?

前端 未结 6 1575
情歌与酒
情歌与酒 2020-11-29 18:45

How would I mock out the database in my node.js application, which in this case uses mongodb as the backend for a blog REST API ?

Sure, I could set the

6条回答
  •  遥遥无期
    2020-11-29 19:17

    There is a general rule of thumb when it comes to mocking which is

    Don't mock anything you don't own.

    If you want to mock out the db hide it behing an abstracted service layer and mock that layer. Then make sure you integration test the actual service layer.

    Personally I've gone away from using mocks for testing and use them for top to bottom design helping me drive development from the top towards the bottom mocking out service layers as I go and then eventually implementing those layers and writing integration tests. Used as a test tool they tend to make your test very brittle and in the worst case leads to a divergence between actual behavior and mocked behavior.

提交回复
热议问题