开发者

ServiceLocator not initialized in Tests project

开发者 https://www.devze.com 2023-03-15 02:12 出处:网络
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:

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() at SharpArch.NHibernate.SessionFactoryKeyHelper.GetKeyFrom(Object anObject) at SharpArch.NHibernate.NHibernateRepositoryWithTypedId2.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.SafeServiceLocator1.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?

0

精彩评论

暂无评论...
验证码 换一张
取 消