views:

249

answers:

1

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

A: 

The authors of S#arp Architecture and WhoCanHelpMe? (WCHM) both use the Common Service Locator to supply an implementation of SharpArch.Core.CommonValidator.IValidator to their validation classes.

Where S#arp Architecture uses the following in their SafeServiceLocator<TDependency> class


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

WCHM uses the following in their ValidatableValueObject class


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

The folks that wrote WCHM only stubbed out the GetInstance<IValidator>() method for their unit test


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

So, if changing to a S#arp Entity from the WCHM ValidatableValueObject its also necessary to stub out the GetService(typeof(IValidator)) that S#arp is going to use.

tlum