When to use a Mocking Framework?

后端 未结 6 571
挽巷
挽巷 2021-01-02 18:52

So I am playing around with mocking frameworks (Moq) for my unit tests, and was wondering when you should use a mocking framework?

What is the benefit/disadvantage b

6条回答
  •  清歌不尽
    2021-01-02 19:35

    Use mock objects to truly create a unit test--a test where all dependencies are assumed to function correctly and all you want to know is if the SUT (system under test--a fancy way of saying the class you're testing) works.

    The mock objects help to "guarantee" your dependencies function correctly because you create mock versions of those dependencies that produce results you configure. The question then becomes if the one class you're testing behaves as it should when everything else is "working."

    Mock objects are particularly critical when you are testing an object with a slow dependency--like a database or a web service. If you were to really hit the database or make the real web service call, your test will take a lot more time to run. That's tolerable when it's only a few extra seconds, but when you have hundreds of tests running in a continuous integration server, that adds up really fast and cripples your automation.

    This is what really makes mock objects important--reducing the build-test-deploy cycle time. Making sure your tests run fast is critical to efficient software development.

提交回复
热议问题