Why do we need mocking frameworks?

后端 未结 11 1762
清歌不尽
清歌不尽 2020-12-30 03:00

I have worked with code which had NUnit test written. But, I have never worked with mocking frameworks. What are they? I understand dependency injection and how it helps to

11条回答
  •  滥情空心
    2020-12-30 03:11

    Mock objects take the place of any large/complex/external objects your code needs access to in order to run.

    They are beneficial for a few reasons:

    • Your tests are meant to run fast and easily. If your code depends on, say, a database connection then you would need to have a fully configured and populated database running in order to run your tests. This can get annoying, so you create a replace - a "mock" - of the database connection object that just simulates the database.

    • You can control exactly what output comes out of the Mock objects and can therefore use them as controllable data sources to your tests.

    • You can create the mock before you create the real object in order to refine its interface. This is useful in Test-driven Development.

提交回复
热议问题