开发者

Moq unit test: "The type initializer for 'Real.Shared.Dependency' threw an exception."

I need help with my Moq tests. I have one big project, and when we started to create tests using XML configuration it all went good. Now that was changed and all configuration is in a DB.

Some methods are changed and so. (I use current creates unit test platform and create some test on. I want to say I am a new one with tests.) Now I have problem: Not even a single test works. I got the next error using some simple test running when code execution try to start method.

if (!UnitWork.HasStarted) UnitWork.Start();

Error:

The type initializer for 'Real.Shared.Dependency' threw an exception.

StackTrace:

at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
at 开发者_开发问答System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at Real.Shared.Dependency..cctor()

Should I need to make changes in test base class, or does someone knows what else happens here?

This is the method where the test breaks when I try to start UnitWork.Start(), which is btw. public static class:

public T ResolveConfValueForURL<T>(string url, string configurationKey)
{
    try
    {
        if (!UnitWork.HasStarted) UnitWork.Start();

        UrlToOrganMapping urlToOrgMap =
            (UrlToOrganMapping)Dependency.Resolve<IUrlToOrganMappingRepository>()
            .GetByUrl( url );

        if ( urlToOrgMap != null )
            return ResolveConfValue<T>(urlToOrgMap.Organization, configurationKey, null);
        else
            return ResolveConfValue<T>(null, configurationKey, null);
    }
    catch (Exception e)
    {
        //Log an eror
        LoggingBLL.LogMessage("Configuration - ResolveConfValueForURL", e);
        throw;
    }
    finally
    {
        if (UnitWork.HasStarted) UnitWork.Finish();
    }
}


Looking at the error message:

at Real.Shared.Dependency..cctor()

.cctor means this is a static constructor, which matches the "type initializer" portion of the error message.

I'm not finding anything on Google for Real.Shared.Dependency; is that one of your classes? If so I would check the static constructor for that type. If necessary put a breakpoint there and run the unit tests; see if any null values are dereferenced.

Try to write the simplest meaningful test you can. Trace through it. If necessary post the code for the test method (including SetUp if any). If Dependency is your class note that and post the static constructor code. Please make an effort to figure this out yourself first; you have access to the full source and a debugger, which we don't.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜