system.nullReferenceException Whocanhelpmeユニットテスト
質問
私は、Sharp Architecture、NhibernateValidatorなどに基づいたWhocanhelpmeに基づいたテストプロジェクトと協力しています。
これで、プロファイルオブジェクトは、ivalivalidatableインターフェイスを継承する独自の有効なValueObjectに由来するCreateProfileDetailsタイプです。
問題は、私のクラスが有効なValueObjectではなくエンティティに基づいているときに表面化します。テストがsystem.nullReferenceExceptionを実行するとき、バリデーターはnullです。
私はこの悪い行動を解決するために迷っているのではないかと心配しています。誰かがこれの底に到達するためにいくつかの提案を持っていますか?
ありがとう、
-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>
検証登録のソースコード:(申し訳ありませんが、新しいユーザーであり、これをリンクとして投稿できないので、コピー/貼り付けます)
解決
s#arpアーキテクチャと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);
したがって、s#arpに変更する場合 Entity
WCHMから ValidatableValueObject
それを突き刺すためにも必要です GetService(typeof(IValidator))
そのS#arpが使用されます。