RhinoAutoMocker Testcase fail due to Method Attribute

45 Views Asked by At

I have an interface

public interface IMyInterface 
{    
    [CustomAttribute]    
    void MethodA();
}

and the corresponding class implementing it

public class MyClass: IMyInterface 
{ 
    public void MethodA()
    { 
        //<some logic>
    } 
}

I am using StructureMap.AutoMocking, when I create a Test case :

var mocker = new RhinoAutoMocker<IMyInterface>(MockMode.AAA);

it throws an exception along the lines of -

System.TypeInitializationException' in MY DLL NAME

Additionally the type initializer for CustomAttribute threw an exception.

public class CustomAttribute : AuthorizeAttribute 
{ 
    internal bool TestAuthorizationIsEnabled;

    internal bool IsAuthorized(string name) 
    { 
        var auth = new Auth();  
        var val = auth.IsAuthorized(name, Roles); 
        return val; 
    }
}

How to fix this error?

1

There are 1 best solutions below

0
On

I'm not sure the issue is related to your custom attribute. It seems that the issue is with using the RhinoAutoMocker API, as it was designed to mock concrete classes and not interfaces with the c'tor. You can get instance of your interface as follows:

 var concreteMock = new RhinoAutoMocker<MyClass>(MockMode.AAA);
 var interfaceMock = mocker.Get<IMyInterface>();