How do you mock a Sealed class?

前端 未结 10 2335
鱼传尺愫
鱼传尺愫 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:39

    I believe that Moles, from Microsoft Research, allows you to do that. From the Moles page:

    Moles may be used to detour any .NET method, including non-virtual/static methods in sealed types.

    UPDATE: there is a new framework called "Fakes" in the upcoming VS 11 release that is designed to replace Moles:

    The Fakes Framework in Visual Studio 11 is the next generation of Moles & Stubs, and will eventually replace it. Fakes is different from Moles, however, so moving from Moles to Fakes will require some modifications to your code. A guide for this migration will be available at a later date.

    Requirements: Visual Studio 11 Ultimate, .NET 4.5

提交回复
热议问题