How do you mock a Sealed class?

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

    The problem with TypeMock is that it excuses bad design. Now, I know that it is often someone else's bad design that it's hiding, but permitting it into your development process can lead very easily to permitting your own bad designs.

    I think if you're going to use a mocking framework, you should use a traditional one (like Moq) and create an isolation layer around the unmockable thing, and mock the isolation layer instead.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-27 15:41

    I came across this problem recently and after reading / searching web, seems like there is no easy way around except to use another tool as mentioned above. Or crude of handling things as I did:

    • Create instance of sealed class without getting constructor called.
    • System.Runtime.Serialization.FormatterServices.GetUninitializedObject(instanceType);

    • Assign values to your properties / fields via reflection

    • YourObject.GetType().GetProperty("PropertyName").SetValue(dto, newValue, null);
    • YourObject.GetType().GetField("FieldName").SetValue(dto, newValue);
    0 讨论(0)
  • 2020-11-27 15:44

    It is perfectly reasonable to mock a sealed class because many framework classes are sealed.

    In my case I'm trying to mock .Net's MessageQueue class so that I can TDD my graceful exception handling logic.

    If anyone has ideas on how to overcome Moq's error regarding "Invalid setup on a non-overridable member", please let me know.

    code:

        [TestMethod]
        public void Test()
        {
            Queue<Message> messages = new Queue<Message>();
            Action<Message> sendDelegate = msg => messages.Enqueue(msg);
            Func<TimeSpan, MessageQueueTransaction, Message> receiveDelegate =
                (v1, v2) =>
                {
                    throw new Exception("Test Exception to simulate a failed queue read.");
                };
    
            MessageQueue mockQueue = QueueMonitorHelper.MockQueue(sendDelegate, receiveDelegate).Object;
        }
        public static Mock<MessageQueue> MockQueue
                    (Action<Message> sendDelegate, Func<TimeSpan, MessageQueueTransaction, Message> receiveDelegate)
        {
            Mock<MessageQueue> mockQueue = new Mock<MessageQueue>(MockBehavior.Strict);
    
            Expression<Action<MessageQueue>> sendMock = (msmq) => msmq.Send(It.IsAny<Message>()); //message => messages.Enqueue(message);
            mockQueue.Setup(sendMock).Callback<Message>(sendDelegate);
    
            Expression<Func<MessageQueue, Message>> receiveMock = (msmq) => msmq.Receive(It.IsAny<TimeSpan>(), It.IsAny<MessageQueueTransaction>());
            mockQueue.Setup(receiveMock).Returns<TimeSpan, MessageQueueTransaction>(receiveDelegate);
    
            return mockQueue;
        }
    
    0 讨论(0)
提交回复
热议问题