0

我正在使用基于WhoCanHelpMe的测试项目,该项目基于Sharp Architecture,NHibernateValidator等。正如其编写的那样,when_the_profile_tasks_is_asked_to_create_a_profile单元测试会创建配置文件对象并将其保存而不会出现问题。System.NullReferenceException与WhoCanHelpMe单元测试

现在,配置文件对象是一个CreateProfileDetails类型,它派生自它们自己的继承了IValidatable接口的ValidatableValueObject。

当我的类基于实体而不是它们的ValidatableValueObject时,问题就出现了。当测试运行时由于Validator为null而导致System.NullReferenceException。

恐怕我无法解决这种不良行为。有没有人有一些建议,以达到这个底部?

感谢,

-Ted-

这是堆栈跟踪:


should ask the question repository to save the new question : FailedObject reference not set to an instance of an object. 
System.NullReferenceException: Object reference not set to an instance of an object. 
at SharpArch.Core.DomainModel.ValidatableObject.IsValid() 
at FieldAudit.Framework.Validation.ValidatableExtensions.Validate(IValidatable entity) in ValidatableExtensions.cs: line 33 
at FieldAudit.Tasks.QuestionTasks.CreateQuestion(Question question) in QuestionTasks.cs: line 40 
at MSpecTests.FieldAudit.Tasks.when_the_question_tasks_is_asked_to_create_a_question.b__2() in QuestionTasksSpecs.cs: line 137 

这是类层次结构:


entity = {FieldAudit.Domain.Question} 
[FieldAudit.Domain.Question] = {FieldAudit.Domain.Question} 
    base {SharpArch.Core.DomainModel.Entity} = {FieldAudit.Domain.Question} 
    base {SharpArch.Core.DomainModel.EntityWithTypedId} = {FieldAudit.Domain.Question} 
     base {SharpArch.Core.DomainModel.ValidatableObject} = {FieldAudit.Domain.Question} 
     Validator = null 
     base {SharpArch.Core.DomainModel.BaseObject} = {FieldAudit.Domain.Question} 

源代码是在这里的http:/ /code.google.com/p/sharp-architecture/source/browse/trunk/src/SharpArch/SharpArch.Core/DomainModel/ValidatableObject。 cs>

验证注册的源代码:(抱歉,我是新用户,无法将其作为链接发布,因此您必须复制/粘贴)whocanhelpme.codeplex.com/SourceControl/changeset/view/58203#883241

+0

堆栈跟踪将非常有帮助。什么引用确切是空?从你写的东西看来,好像某些'Validate()'方法是null,这对于C#来说是不可能的。 – 2010-05-07 12:50:45

+0

我已经足够远看到ServiceLocator返回null,但不会抛出任何异常。这最终会导致null Validator引用。 – tlum 2010-05-09 01:44:52

回答

1

S#arp Architecture和WhoCanHelpMe的作者? (WCHM)都使用通用服务定位器向其验证类提供SharpArch.Core.CommonValidator.IValidator的实现。

其中S#ARP架构使用它们SafeServiceLocator<TDependency>


service = (TDependency)ServiceLocator.Current.GetService(typeof(TDependency)); 

WCHM以下使用在他们的ValidatableValueObject类以下


return ServiceLocator.Current.GetInstance<IValidator>(); 

是写WCHM只有掐灭了GetInstance<IValidator>()方法的人他们的单元测试


      var validator = new Validator(); 
      provider.Stub(p => p.GetInstance<IValidator>()).Return(validator); 

因此,如果从WCHM ValidatableValueObject更改为S#arp Entity,则还需要对S#arp将要使用的GetService(typeof(IValidator))进行剔除。