Why do we need mocking frameworks?

后端 未结 11 1761
清歌不尽
清歌不尽 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:34

    Mocking frameworks allow you to isolate units of code that you wish to test from that code's dependencies. They also allow you to simulate various behaviors of your code's dependencies in a test environment that might be difficult to setup or reproduce otherwise.

    For example if I have a class A containing business rules and logic that I wish to test, but this class A depends on a data-access classes, other business classes, even u/i classes, etc., these other classes can be mocked to perform in a certain manner (or in no manner at all in the case of loose mock behavior) to test the logic within your class A based on every imaginable way that these other classes could conceivably behave in a production environment.

    To give a deeper example, suppose that your class A invokes a method on a data access class such as

    public bool IsOrderOnHold(int orderNumber) {}
    

    then a mock of that data access class could be setup to return true every time or to return false every time, to test how your class A responds to such circumstances.

提交回复
热议问题