How do you mock a Sealed class?

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

    I generally take the route of creating a an interface and adaptor/proxy class to facilitate mocking of the sealed type. However, I've also experimented with skipping creation of the interface and making the proxy type non-sealed with virtual methods. This worked well when the proxy is really a natural base class that encapsulates and users part of the sealed class.

    When dealing with code that required this adaptation, I got tired of performing the same actions to create the interface and proxy type so I implemented a library to automate the task.

    The code is somewhat more sophisticated than the sample given in the article you reference, as it produces an assembly (instead of source code), allows for code generation to be performed on any type, and doesn't require as much configuration.

    For more information, please refer to this page.

提交回复
热议问题