English 中文(简体)
未在测试项目中开标的投稿人
原标题:ServiceLocator not initialized in Tests project

在试图撰写与我的新任务有关的测试(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.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:codeMyProjectSolutionsMyProject.TestsMyProject.TasksCategoryTasksTests.cs:line 36 --NullReferenceException at Microsoft.Practices.ServiceLocation.ServiceLocator.get_Current() at SharpArch.Domain.SafeServiceLocator1.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));
}
}
最佳回答

你们正在登记DefaultSessionFactoryKeyProvider,作为DefaultSession FactoryKeyProvider的实施工作,而你则依赖ISessionFactoryKeyProvider,因为没有为这一接口登记任何执行情况,该掩饰家知道如何解决。

我认为应该:

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

问题回答

伊斯堡。 测试项目核心参考吗?





相关问题
How do I ignore a test based on another test in NUnit?

I m writing some NUnit tests for database operations. Obviously, if Add() fails, then Get() will fail as well. However, it looks deceiving when both Add() and Get() fail because it looks like there s ...

result.viewname is always string.empty

I cannot seem to return the result.ViewName for use in Nunit tests as it always returns string.empty. I have explicitly set the name of the view inside my controller and would expect the test to pick ...

C#: How to test a basic threaded worker class

I m dipping my toes in how to test multi-threaded stuff, but not quite sure how to get started. I m sure I will figure more stuff out easier if I could just get stuff going, so I was wondering if ...

Selenium Grid with parallel testing using C#/NUnit

I ve got several unit tests written with NUnit that are calling selenium commands. I ve got 2 win2k3 server boxes setup, one is running selenium grid hub along with 2 selenium rc s. The other box is ...

Code coverage with nUnit? [closed]

Is there a way to see the code coverage when using nUnit? I know there s such a feature in Visual Studio, but can you use it with nUnit or only with the built-in vs unit tests?

Unit testing database-dependent Window services

We have a set of services in .NET 3.5C# and WCF. The NUnit tests need the services to be running and listening for requests. The services need an updated SQL database to be ready for connection. ...

TeamCity NUnit test result visualisation

Is there any way to produce visual results of NUnit tests from within TeamCity s "Tests" tab, currently my NAnt script outputs an .xml file of the results using the following task: <nunit2 ...

热门标签