Why not hit the database inside unit tests?

后端 未结 7 1871
被撕碎了的回忆
被撕碎了的回忆 2021-01-04 09:16

I\'ve read in blogs that the database should not be hit when the unit tests run. I understand the theory, however say i have complex store procedures that are part of a busi

7条回答
  •  耶瑟儿~
    2021-01-04 09:48

    "...the database should not be hit when the unit tests run..." - unless you're unit testing persistence objects, of course.

    I don't know which blogs you're citing, but I'll bet what they're saying is that mocking some components during integration testing can have benefits. If you've already unit tested the persistence tier, there's no need to test it again. The benefit of mocking in that case has more to do with reducing dependencies and uncertainty. For example, you might have unit tested your code, packaged it up, and now it's time to move it into an environment that's closer to production. If the databases you need can't be available (e.g., don't have the proper version of the schema, missing data that you need, or simply needed by another application that got there first), you can use mocks to allow your integration test to proceed without delay.

提交回复
热议问题