Why do we need mocking frameworks?

后端 未结 11 1739
清歌不尽
清歌不尽 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: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>();
      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.

提交回复
热议问题