How do I mock a class without an interface?

前端 未结 8 1240
[愿得一人]
[愿得一人] 2020-11-30 23:50

I am working on .NET 4.0 using C# in Windows 7.

I want to test the communication between some methods using mock. The only problem is that I want to do it without i

相关标签:
8条回答
  • 2020-12-01 00:13

    If you cannot change the class under test, then the only option I can suggest is using MS Fakes https://msdn.microsoft.com/en-us/library/hh549175.aspx. However, MS Fakes works only in a few editions of Visual Studio.

    0 讨论(0)
  • 2020-12-01 00:21

    The standard mocking frameworks are creating proxy classes. This is the reason why they are technically limited to interfaces and virtual methods.

    If you want to mock 'normal' methods as well, you need a tool that works with instrumentation instead of proxy generation. E.g. MS Moles and Typemock can do that. But the former has a horrible 'API', and the latter is commercial.

    0 讨论(0)
  • 2020-12-01 00:27

    I faced something like that in one of the old and legacy projects that i worked in that not contains any interfaces or best practice and also it's too hard to enforce them build things again or refactoring the code due to the maturity of the project business, So in my UnitTest project i used to create a Wrapper over the classes that I want to mock and that wrapper implement interface which contains all my needed methods that I want to setup and work with, Now I can mock the wrapper instead of the real class.

    For Example:

    Service you want to test which not contains virtual methods or implement interface

    public class ServiceA{
    
    public void A(){}
    
    public String B(){}
    
    }
    

    Wrapper to moq

    public class ServiceAWrapper : IServiceAWrapper{
    
    public void A(){}
    
    public String B(){}
    
    }
    

    The Wrapper Interface

    public interface IServiceAWrapper{
    
    void A();
    
    String B();
    
    }
    

    In the unit test you can now mock the wrapper:

        public void A_Run_ChangeStateOfX()
        {
        var moq = new Mock<IServiceAWrapper>();
        moq.Setup(...);
        }
    

    This might be not the best practice, but if your project rules force you in this way, do it. Also Put all your Wrappers inside your Unit Test project or Helper project specified only for the unit tests in order to not overload the project with unneeded wrappers or adaptors.

    Update: This answer from more than a year but in this year i faced a lot of similar scenarios with different solutions. For example it's so easy to use Microsoft Fake Framework to create mocks, fakes and stubs and even test private and protected methods without any interfaces. You can read: https://docs.microsoft.com/en-us/visualstudio/test/isolating-code-under-test-with-microsoft-fakes?view=vs-2017

    0 讨论(0)
  • 2020-12-01 00:30

    With MoQ, you can mock concrete classes:

    var mocked = new Mock<MyConcreteClass>();
    

    but this allows you to override virtual code (methods and properties).

    0 讨论(0)
  • 2020-12-01 00:34

    I think it's better to create an interface for that class. And create a unit test using interface.

    If it you don't have access to that class, you can create an adapter for that class.

    For example:

    public class RealClass
    {
        int DoSomething(string input)
        {
            // real implementation here
        }
    }
    
    public interface IRealClassAdapter
    {
        int DoSomething(string input);
    }
    
    public class RealClassAdapter : IRealClassAdapter
    {
        readonly RealClass _realClass;
    
        public RealClassAdapter() => _realClass = new RealClass();
    
        int DoSomething(string input) => _realClass.DoSomething(input);
    }
    

    This way, you can easily create mock for your class using IRealClassAdapter.

    Hope it works.

    0 讨论(0)
  • 2020-12-01 00:34

    If worse comes to worse, you can create an interface and adapter pair. You would change all uses of ConcreteClass to use the interface instead, and always pass the adapter instead of the concrete class in production code.

    The adapter implements the interface, so the mock can also implement the interface.

    It's more scaffolding than just making a method virtual or just adding an interface, but if you don't have access to the source for the concrete class it can get you out of a bind.

    0 讨论(0)
提交回复
热议问题