Mock Objects vs Test Database

后端 未结 6 1867
庸人自扰
庸人自扰 2021-02-19 20:42

What\'s the advantage of using mock objects in comparison to a static test database that has known data and using transactions to make sure nothing changes when testing against

6条回答
  •  失恋的感觉
    2021-02-19 21:00

    Usually you would use both of these approaches.

    For unit tests you would use mocked objects. This way you can test your system in a much more fine-grained way, because you can mock every object - not only the ones that wrap the database connection. In general it is good practise to unit test every class in separation of all the dependencies. The benefits are - its is possible to test all the error handling on all levels and make sure you test all the code paths, when you get a test failure you can immediately find the cause, etc.

    For integration and end-to-end tests you test big parts of the system or the whole thing. Then you would connect to the database (because this connection is part of the test). Obviously you have to ensure that the database is in a known state, etc., etc.

    You will end up having much more unit tests than integration tests, so its actually quite important to make them very quick - yet another advantage of using mock objects.

提交回复
热议问题