.NET Layout Classes Using Shell Classes

I have a class that takes an instance of MethodInfo and extracts some information from it, but I would like to mock this class. This is complicated at the moment, because it accepts MethodInfo, so I planned to create a wrapper for the MethodInfo class and implement an interface on it. For instance:

public interface IMethodInfo
{
    string Name { get; }
}

public class MethodInfoProxy : IMethodInfo
{
    private readonly MethodInfo _method;
    public MethodInfoProxy(MethodInfo method)
    {
        _method = method;
    }

    public string Name { get { return _method.Name; } }
}

public class MyClass
{
    public MyClass(IMethodInfo method)
    {
        ...
    }
}

Another example is the File.Exists method. The idea would be to create IFile.Exists and put it in the FileProxy class, which would simply delegate File.Exists.

Since I am new to the world of unit testing, I would like to know if this would be considered a good approach?

+5
source share
5 answers

:

, , , , . , , , API. - .

+3

- MethodInfo ( ) Name.

, -, ( ), , TypeMock Moles ( : ). ​​

-, SystemWrapper, .NET.

+1

, , . , ​​ Microsoft http://research.microsoft.com/en-us/projects/pex/, .

0

Mock ( ) , , , .

public interface IMethodInfo
{
    string Name { get; }
}

:

FakeMethodInfo : IMethodInfo
{
 string Name {get {return "FakeMethod";}}
}

, unit test, FakeMethodInfo, IMethodInfo.

, , FakeMethodInfo , , - , .


, . :

interface IUser
{
 string UserName {get;}
}

, .

, , , , , `IUser.

Take a look at this great answer. I wrote why you will mock. And an example with Moq .:

Difference between dependency injection and Mocking system (Ninject vs RhinoMock or Moq)

0
source

For faster creation of wrapper classes, you can use one of the forest code generators I created.

https://www.nuget.org/packages/Digitrish.WrapperGenerator/

This will lead to the creation of an interface that you can use for any laughing framework and a specific wrapper class for a real implementation.

0
source

All Articles