How do you mock a Sealed class?

前端 未结 10 2346
鱼传尺愫
鱼传尺愫 2020-11-27 15:27

Mocking sealed classes can be quite a pain. I currently favor an Adapter pattern to handle this, but something about just keeps feels weird.

So, What is t

10条回答
  •  广开言路
    2020-11-27 15:33

    Although its currently only available in beta release, I think its worthwhile keeping in mind the shim feature of the new Fakes framework (part of the Visual Studio 11 Beta release).

    Shim types provide a mechanism to detour any .NET method to a user defined delegate. Shim types are code-generated by the Fakes generator, and they use delegates, which we call shim types, to specify the new method implementations. Under the hood, shim types use callbacks that were injected at runtime in the method MSIL bodies.

    Personally I was looking at using this to mock the methods on sealed framework classes such as DrawingContext.

提交回复
热议问题