ServiceLocator 未在测试项目中初始化
当尝试编写与我的新任务(MVC3、S#arp 2.0)相关的测试时,我在尝试运行测试时收到此错误:
MyProject.Tests.MyProject.Tasks.CategoryTasksTests.CanConfirmDeleteReadiness: 设置:System.NullReferenceException : ServiceLocator 尚未 已初始化;我试图找回 SharpArch.NHibernate.ISessionFactoryKeyProvider ----> System.NullReferenceException:对象引用未设置为 对象的实例。
在 SharpArch.Domain.SafeServiceLocator
1.GetService() 在 SharpArch.NHibernate.SessionFactoryKeyHelper.GetKeyFrom(对象 一个对象)在 SharpArch.NHibernate.NHibernateRepositoryWithTypedId
2.get_Session() 在 SharpArch.NHibernate.NHibernateRepositoryWithTypedId2.Save(T 实体)在 MyProject.Tests.MyProject.Tasks.CategoryTasksTests.Setup() 在 C:\code\MyProject\Solutions\MyProject.Tests\MyProject.Tasks\CategoryTasksTests.cs:line 36 --Microsoft.Practices.ServiceLocation.ServiceLocator.get_Current() 处的 NullReferenceException 在 SharpArch.Domain.SafeServiceLocator
1.GetService()
不涉及新类的其他测试(例如,生成/确认数据库映射)正确运行。
我的ServiceLocatorInitializer如下
public class ServiceLocatorInitializer
{
public static void Init()
{
IWindsorContainer container = new WindsorContainer();
container.Register(
Component
.For(typeof(DefaultSessionFactoryKeyProvider))
.ImplementedBy(typeof(DefaultSessionFactoryKeyProvider))
.Named("sessionFactoryKeyProvider"));
container.Register(
Component
.For(typeof(IEntityDuplicateChecker))
.ImplementedBy(typeof(EntityDuplicateChecker))
.Named("entityDuplicateChecker"));
ServiceLocator.SetLocatorProvider(() => new WindsorServiceLocator(container));
}
}
When attempting to write a test related to my new Tasks (MVC3, S#arp 2.0), I get this error when I try to run the test:
MyProject.Tests.MyProject.Tasks.CategoryTasksTests.CanConfirmDeleteReadiness:
SetUp : System.NullReferenceException
: ServiceLocator has not been
initialized; I was trying to retrieve
SharpArch.NHibernate.ISessionFactoryKeyProvider
----> System.NullReferenceException : Object reference not set to an
instance of an object.at
SharpArch.Domain.SafeServiceLocator1.GetService()
2.get_Session()
at
SharpArch.NHibernate.SessionFactoryKeyHelper.GetKeyFrom(Object
anObject) at
SharpArch.NHibernate.NHibernateRepositoryWithTypedId
at
SharpArch.NHibernate.NHibernateRepositoryWithTypedId2.Save(T
1.GetService()
entity) at
MyProject.Tests.MyProject.Tasks.CategoryTasksTests.Setup()
in
C:\code\MyProject\Solutions\MyProject.Tests\MyProject.Tasks\CategoryTasksTests.cs:line
36
--NullReferenceException at Microsoft.Practices.ServiceLocation.ServiceLocator.get_Current()
at
SharpArch.Domain.SafeServiceLocator
Other tests which do not involve the new class (e.g., generate/confirm database mappings) run correctly.
My ServiceLocatorInitializer is as follows
public class ServiceLocatorInitializer
{
public static void Init()
{
IWindsorContainer container = new WindsorContainer();
container.Register(
Component
.For(typeof(DefaultSessionFactoryKeyProvider))
.ImplementedBy(typeof(DefaultSessionFactoryKeyProvider))
.Named("sessionFactoryKeyProvider"));
container.Register(
Component
.For(typeof(IEntityDuplicateChecker))
.ImplementedBy(typeof(EntityDuplicateChecker))
.Named("entityDuplicateChecker"));
ServiceLocator.SetLocatorProvider(() => new WindsorServiceLocator(container));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将 DefaultSessionFactoryKeyProvider 注册为 DefaultSessionFactoryKeyProvider 的实现,同时您依赖于 ISessionFactoryKeyProvider,温莎城堡不知道如何解析,因为尚未为该接口注册任何实现。
我认为应该是:
container.Register(
成分
.For(typeof(ISessionFactoryKeyProvider))
.ImplementedBy(typeof(DefaultSessionFactoryKeyProvider))
.Named("sessionFactoryKeyProvider"));
You are registering DefaultSessionFactoryKeyProvider as an implementation of DefaultSessionFactoryKeyProvider, while you have a dependency on ISessionFactoryKeyProvider, which castle windsor doesn't know how to resolve as no implementations has been registered for that interface.
I think that should be:
container.Register(
Component
.For(typeof(ISessionFactoryKeyProvider))
.ImplementedBy(typeof(DefaultSessionFactoryKeyProvider))
.Named("sessionFactoryKeyProvider"));
您的测试项目是否引用了 Castle.Core?
Is Castle.Core referenced by your test project?