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 = ne开发者_开发技巧w 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"));
Is Castle.Core referenced by your test project?
精彩评论