服务定位在测试项目中未初始化 [英] ServiceLocator not initialized in Tests project
问题描述
当试图写入与我的新任务(MVC3,S#ARP 2.0)测试,当我尝试运行测试,我得到这个错误:
MyProject.Tests.MyProject.Tasks.CategoryTasksTests.CanConfirmDeleteReadiness:
的SetUp信息:System.NullReferenceException
:服务定位尚未
初始化;我试图找回
SharpArch.NHibernate.ISessionFactoryKeyProvider
----> System.NullReferenceException:对象未设置为一个
实例的对象。
在
SharpArch.Domain.SafeServiceLocator1.GetService()
2.get_Session()
在
SharpArch.NHibernate.SessionFactoryKeyHelper.GetKeyFrom(对象
anObject)在
SharpArch.NHibernate.NHibernateRepositoryWithTypedId
在
SharpArch.NHibernate.NHibernateRepositoryWithTypedId2.Save(T
1.GetService()
实体)在
MyProject.Tests.MyProject.Tasks.CategoryTasksTests.Setup()
在
C:\\ code \\ MyProject的\\解决方案\\ MyProject.Tests \\ MyProject.Tasks \\ CategoryTasksTests.cs:行
36
--NullReferenceException在Microsoft.Practices.ServiceLocation.ServiceLocator.get_Current()
在
SharpArch.Domain.SafeServiceLocator
块引用>不涉及新类其他测试(例如,生成/确认数据库映射)正常运行。
我ServiceLocatorInitializer如下:
公共类ServiceLocatorInitializer
{公共静态无效的init()
{
IWindsorContainer集装箱=新WindsorContainer(); container.Register(
零件
。对于(typeof运算(DefaultSessionFactoryKeyProvider))
.ImplementedBy(typeof运算(DefaultSessionFactoryKeyProvider))
.Named(sessionFactoryKeyProvider)); container.Register(
零件
。对于(typeof运算(IEntityDuplicateChecker))
.ImplementedBy(typeof运算(EntityDuplicateChecker))
.Named(entityDuplicateChecker)); ServiceLocator.SetLocatorProvider(()=>新建WindsorServiceLocator(集装箱));
}
}
解决方案您正在注册DefaultSessionFactoryKeyProvider作为DefaultSessionFactoryKeyProvider的实现,同时你有ISessionFactoryKeyProvider的依赖,这温莎城堡不知道如何解决,因为没有实现已注册该接口。
我觉得应该是:
container.Register(
零件
。对于(typeof运算(ISessionFactoryKeyProvider))
.ImplementedBy(typeof运算(DefaultSessionFactoryKeyProvider))
.Named(sessionFactoryKeyProvider));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.SafeServiceLocator
1.GetService() at SharpArch.NHibernate.SessionFactoryKeyHelper.GetKeyFrom(Object anObject) at SharpArch.NHibernate.NHibernateRepositoryWithTypedId
2.get_Session() at SharpArch.NHibernate.NHibernateRepositoryWithTypedId2.Save(T 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
1.GetService()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)); } }
解决方案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"));
这篇关于服务定位在测试项目中未初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!