Moq 单元测试:“‘Real.Shared.Dependency’的类型初始值设定项;抛出异常。”
我需要有关起订量测试的帮助。我有一个大项目,当我们开始使用 XML 配置创建测试时,一切都很顺利。现在一切都改变了,所有配置都在数据库中。
有些方法改变了等等。 (我使用当前创建的单元测试平台并创建一些测试。我想说我是一个有测试的新人。)现在我遇到了问题:甚至没有一个测试有效。当代码执行尝试启动方法时,我使用一些简单的测试运行得到了下一个错误。
if (!UnitWork.HasStarted) UnitWork.Start();
错误:
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()
我是否需要在测试基类中进行更改,或者有人知道这里还会发生什么吗?
这是当我尝试启动 UnitWork.Start()
时测试中断的方法,顺便说一句。 公共静态类
:
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();
}
}
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看错误信息:
.cctor 表示这是一个静态构造函数,它与错误消息的“类型初始值设定项”部分匹配。
我在 Google 上没有找到任何关于
Real.Shared.Dependency
的信息;那是你的一门课吗?如果是这样,我会检查该类型的静态构造函数。如有必要,在此处放置一个断点并运行单元测试;查看是否取消引用任何空值。尝试编写最简单、有意义的测试。追踪它。如有必要,发布测试方法的代码(包括 SetUp,如果有)。如果 Dependency 是您的类,请注意并发布静态构造函数代码。请先努力自己弄清楚这一点;您可以访问完整的源代码和调试器,但我们没有。
Looking at the error message:
.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.