System.NullReference Exception带有Whocanhelpme单元测试
题
我正在使用基于whocanhelpme的测试项目,该项目基于尖锐的体系结构,nhibernateValidator等。作为书面_the_profile_istasks_is_asked_to_create_create_a_a_a_profile单位测试创建配置文件对象并无问题保存。
现在,配置文件对象是一种createprofiledetails类型,它从其自己的valialableValueObject衍生而来,它继承了ivalidatable接口。
当我的班级基于实体而不是其有效的valueObject时,问题会浮出水面。测试运行时,系统。NullReferenceException,因为验证器为null。
恐怕我不知所措地解决这种不良行为。有人有一些建议以达到最底层吗?
谢谢,
-
这是堆栈跟踪:
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/src/sharparch/sharparch/sharparch.core/domainmodel/validatablebigt.cs>
验证注册的源代码:(对不起,我是一个新用户,不能将其发布为链接,因此您必须复制/粘贴)whocanhelpme.codeplex.com/sourcecontrol/changeset/view/58203#8832241
解决方案
S#ARP Architecture和Whocanhelpme的作者? (WCHM)两者都使用公共服务定位器来提供sharparch.core.come.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);
因此,如果更改为s#arp Entity
来自WCHM ValidatableValueObject
这也必须使 GetService(typeof(IValidator))
S#ARP将使用。