• Home
  • Q&A
  • Moq unit test: "The type initializer for 'Real.Shared.Dependency' threw an exception."
 
5
0
0

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

TolstojviaStackOverflow
March 20, 2011
0 score
1 answers

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();
    }
}

Answers

Discussion

-