在试图撰写与我的新任务有关的测试(MVC3,S#arp 2.0)时,我发现这一错误,我试图进行试验:
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:codeMyProjectSolutionsMyProject.TestsMyProject.TasksCategoryTasksTests.cs:line 36 --NullReferenceException at Microsoft.Practices.ServiceLocation.ServiceLocator.get_Current() at SharpArch.Domain.SafeServiceLocator
1.GetService()
不涉及新类别的其他测试(如生成/绘制数据库图)是正确的。
我的服务提供者如下:
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));
}
}