0

I'm working with a test project based on WhoCanHelpMe, which is based on Sharp Architecture, NHibernateValidator, etc. As its written the when_the_profile_tasks_is_asked_to_create_a_profile unit test creates the profile object and saves it without issue.

Now the profile object is a CreateProfileDetails type that derives from their own ValidatableValueObject which inherits the IValidatable interface.

The problem surfaces when my class is based on an Entity rather than their ValidatableValueObject. When the test is run a System.NullReferenceException because Validator is null.

I'm afraid that I'm at a loss to resolve this bad behavior. Does anyone have some suggestions to get to the bottom of this?

Thanks,

-Ted-

This is the stack trace:


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 

This is the class hierarchy:


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}

Source code is here http://code.google.com/p/sharp-architecture/source/browse/trunk/src/SharpArch/SharpArch.Core/DomainModel/ValidatableObject.cs>

Source code for validation registration: (sorry I'm a new user and can't post this as a link so you'll have to copy/paste) whocanhelpme.codeplex.com/SourceControl/changeset/view/58203#883241

4

1 回答 1

1

S#arp Architecture 和 WhoCanHelpMe? 的作者 (WCHM) 都使用 Common Service Locator 为其验证类提供 SharpArch.Core.CommonValidator.IValidator 的实现。

S#arp Architecture 在他们的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);

Entity因此,如果从 WCHM更改为 S#arp ,则ValidatableValueObject还必须将GetService(typeof(IValidator))S#arp 将要使用的 S#arp 存根。

于 2010-05-09T14:32:15.907 回答