Non Interface dependent Mocking Frameworks for C#

后端 未结 4 1807
刺人心
刺人心 2020-12-25 08:37

I am new to mocking so I might have it totally wrong here but I believe that most mocking frameworks are interface dependent. Unfortunately most of our code is not using an

4条回答
  •  眼角桃花
    2020-12-25 09:08

    A lot of .NET mocking frameworks use Castle Dynamic Proxy to create mocks at runtime. Hence the limitation of only allowing interface/virtual methods to be mocked comes from Castle and I think is rooted in CLR. Both MOQ and RhinoMocks are able to mock virtual methods, which is as good as it gets.

    Both classes and interfaces can be proxied, however only virtual members can be intercepted.

    My advice would be to start creating abstract bases for those classes that need to be mocked and have the concrete class extend it. Then the abstract base can be passed around and mocked. It really is a refactoring exercise that is not overly complex.

提交回复
热议问题