WCF サービスの統合/受け入れテストには、セルフ ホストWCF サービスを使用することをお勧めします。ここにある例:
フィクスチャのセットアップ時にセルフホステッド サービスを作成し、フィクスチャの破棄時にそれを閉じます。
EndpointAddress address = new EndpointAddress("http://localhost:8080/service1");
ServiceHost host;
IService1 service;
[TestFixtureSetUp]
public void FixtureSetUp()
{
var binding = new BasicHttpBinding();
host = new ServiceHost(typeof(Service1), address.Uri);
host.AddServiceEndpoint(typeof(IService1), binding, address.Uri);
host.Open();
}
[TestFixtureTearDown]
public void FixtureTearDown()
{
if (host == null)
return;
if (host.State == CommunicationState.Opened)
host.Close();
else if (host.State == CommunicationState.Faulted)
host.Abort();
}
サービスをホストすると、サービス プロキシを取得できます。
var binding = new BasicHttpBinding();
ChannelFactory<IService1> factory =
new ChannelFactory<IService1>(binding, address);
service = factory.CreateChannel();
テストは次のようになります。
[Test]
public void ShouldReturnSomeStuff()
{
var result = service.GetStuff();
Assert.NotNull(result);
}