オブジェクトグラフがどのように解決されるかをテストする簡単なAPIはありますか?ビューをデバッグする目的で同じように見える診断APIに基づくものでしょうか?
はいこれを行うにはいくつかの方法があります。考えられる2つのオプションは、InstanceProducer. Both allow visualizing object graphs.
GetVelationships gives you a structured graph that you can iterate over, while
VisualizeObjectGraph`のVisualizeObjectGraph
とGetRelationships
メソッドで、オブジェクトグラフを文字列形式で返します。これはC#に似ています。
例:
var container = new Container();
// Do registrations
// You need to verify to get the correct output of those methods
container.Verify();
var r = container.GetRegistration(typeof(A1));
Console.WriteLine(r.VisualizeObjectGraph());
これはGetRelationships()
を使用するときは、再帰的にグラフを反復処理することができますし、指定されたタイプは、登録(サブ)の依存性であるかどうかを確認
A1(
FileLogger(
Dependency1(),
Dependency2()),
C1(
Dependency1(),
SomeService()));
のようなものを出力。登録は書き込みテストのみであろうと、十分な表現力でなければなりません
- :
例:それは私はので、あなたは、登録チェックのこれらの種類を可能な限り制限するべきだと思いますけれども
// Useful extension method
private static IEnumerable<InstanceProducer> GetDependencies(this InstanceProducer p) {
foreach (var r in p.GetRelationships()) {
yield return r.Dependency;
foreach (var d in r.Dependency.GetDependencies()) {
yield return d;
}
}
}
var container = new Container();
// Do registrations
// You need to verify to get the correct output of those methods
container.Verify();
var deps = container.GetRegistration(typeof(A1)).GetDependencies();
Assert.IsTrue(deps.Any(p => p.Registration.ImplementationType == typeof(C1)));
はメモしていますあなたがすでにコンポジションルートで定義したものを繰り返します。
- これらのシナリオの大部分は、機能レベルで既に広範な統合テストでカバーされているはずです。
- シンプルなインジェクタの確認と診断機能は、一般的な誤設定を検出します。コマンドとクエリの記事を見つけることができます興味がある人のための
[ここ](https://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=91)と[ここ](HTTPS ://www.cuttingedge。it/blogs/steven/pivot/entry.php?id = 92) – qujck