Why do we need mocking frameworks?

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

    You certainly can mock your dependencies manually, but with a framework it takes a lot of the tedious work away. Also the assertions usually available make it worth it to learn.

    0 讨论(0)
  • 2020-12-30 03:29
    • It makes mocking easier
    • They usually allow you to express testable assertions that refer to the interaction between objects.

    Here you have an example:

    var extension = MockRepository
        .GenerateMock<IContextExtension<StandardContext>>();
      var ctx = new StandardContext();
      ctx.AddExtension(extension);
      extension.AssertWasCalled(
        e=>e.Attach(null), 
        o=>o.Constraints(Is.Equal(ctx)));
    

    You can see that I explicitly test that the Attach method of the IContextExtension was called and that the input parameter was said context object. It would make my test fail if that did not happen.

    0 讨论(0)
  • 2020-12-30 03:30

    Sometimes when working with third-party libraries, or even working with some aspects of the .NET framework, it is extremely difficult to write tests for some situations - for example, an HttpContext, or a Sharepoint object. Creating mock objects for those can become very cumbersome, so mocking frameworks take care of the basics so we can spend our time focusing on what makes our applications unique.

    0 讨论(0)
  • 2020-12-30 03:30

    Using a mocking framework can be a much more lightweight and simple solution to provide mocks than actually creating a mock object for every object you want to mock.

    For example, mocking frameworks are especially useful to do things like verify that a call was made (or even how many times that call was made). Making your own mock objects to check behaviors like this (while mocking behavior is a topic in itself) is tedious, and yet another place for you to introduce a bug.

    Check out Rhino Mocks for an example of how powerful a mocking framework can be.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题