Unitestsingのヘルプが必要です。 (TypeMockがないものとします)このコードをどうやって統一しますか?
EndpointAddress、DiscoveryEndpoint、DiscoveryClientの代わりにモックを挿入するにはコードを変更しますか?
あなたはどのようなテストを書いていますか?私はあなたの助けのいずれかに感謝
GetService_ServiceExist_ResultShouldBeAnInstanceの
GetService_ServiceIsNotExist_ResultShouldNull
static public T GetService<T>(Binding binding, string address)
{
Contract.Requires(binding != null);
Contract.Requires(!string.IsNullOrWhiteSpace(address));
var endpointAddress= new EndpointAddress(address);
var discoveryEndpoint = new DiscoveryEndpoint(binding, endpointAddress);
var discoveryClient = new DiscoveryClient(discoveryEndpoint);
try
{
// Find ICalculatorService endpoints
FindResponse findResponse = discoveryClient.Find(new FindCriteria(typeof(T)));
Contract.Assume(findResponse != null);
Contract.Assume(findResponse.Endpoints != null);
// Check to see if endpoints were found, if so then invoke the service.););
if (findResponse.Endpoints.Count > 0)
{
Contract.Assume(findResponse.Endpoints[0] != null);
return ChannelFactory<T>.CreateChannel(new BasicHttpBinding(),
findResponse.Endpoints[0].Address);
}
}
catch (TargetInvocationException ex)
{
Console.WriteLine("This client was unable to connect to and query the proxy. Ensure that the proxy is up and running: " + ex);
}
return default(T);
}
を考えることができます。 ありがとう!
アリエル
ここでの依存関係はAPIを汚染するとは思われませんか? – ArielBH
真実にはありません。あなたのシステムの他のコンポーネントに依存しているコード。それで、コンストラクターを介してそれらを渡すことによって、その依存関係を明示的に表現してみませんか?それ以外の場合は手動で作成することもできますが、その場合でも工場を使って作業することを検討します。あなたが本当にあなたがIOCを使う必要はありませんが、何か複雑なものを扱っているなら、私はそれを使うでしょう。 – Simon
Simon、ありがとう。今すぐリファクタリング。私は静的APIから戻ってきます...(私はIoCコンテナを使用しています...) – ArielBH