ServiceLocator 未在测试项目中初始化

发布于 2024-11-17 05:37:00 字数 1765 浏览 3 评论 0原文

当尝试编写与我的新任务(MVC3、S#arp 2.0)相关的测试时,我在尝试运行测试时收到此错误:

MyProject.Tests.MyProject.Tasks.CategoryTasksTests.CanConfirmDeleteReadiness: 设置:System.NullReferenceException : ServiceLocator 尚未 已初始化;我试图找回 SharpArch.NHibernate.ISessionFactoryKeyProvider ----> System.NullReferenceException:对象引用未设置为 对象的实例。

在 SharpArch.Domain.SafeServiceLocator1.GetService() 在 SharpArch.NHibernate.SessionFactoryKeyHelper.GetKeyFrom(对象 一个对象)在 SharpArch.NHibernate.NHibernateRepositoryWithTypedId2.get_Session() 在 SharpArch.NHibernate.NHibernateRepositoryWithTypedId2.Save(T 实体)在 MyProject.Tests.MyProject.Tasks.CategoryTasksTests.Setup() 在 C:\code\MyProject\Solutions\MyProject.Tests\MyProject.Tasks\CategoryTasksTests.cs:line 36 --Microsoft.Practices.ServiceLocation.ServiceLocator.get_Current() 处的 NullReferenceException 在 SharpArch.Domain.SafeServiceLocator1.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));
}
}

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.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));
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

も让我眼熟你 2024-11-24 05:37:00

您将 DefaultSessionFactoryKeyProvider 注册为 DefaultSessionFactoryKeyProvider 的实现,同时您依赖于 ISessionFactoryKeyProvider,温莎城堡不知道如何解析,因为尚未为该接口注册任何实现。

我认为应该是:

container.Register(
成分
.For(typeof(ISessionFactoryKeyProvider))
.ImplementedBy(typeof(DefaultSessionFactoryKeyProvider))
.Named("sessionFactoryKeyProvider"));

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"));

汐鸠 2024-11-24 05:37:00

您的测试项目是否引用了 Castle.Core?

Is Castle.Core referenced by your test project?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文