当尝试写有关我的新任务(MVC3,S#ARP 2.0),当我尝试运行测试,我得到这个错误的测试:
MyProject.Tests.MyProject.Tasks.CategoryTasksTests.CanConfirmDeleteReadiness:SETUP信息:System.NullReferenceException:服务定位尚未初始化; 我试图找回SharpArch.NHibernate.ISessionFactoryKeyProvider ----> System.NullReferenceException:对象不设置到对象的实例。
在SharpArch.Domain.SafeServiceLocator
1.GetService() at SharpArch.NHibernate.SessionFactoryKeyHelper.GetKeyFrom(Object anObject) at SharpArch.NHibernate.NHibernateRepositoryWithTypedId
2.get_Session()在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()
不涉及新类的其他测试(例如,生成/确认数据库映射)正常运行。
我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));
}
}