質問

I would like to write an automated regression test whereby my service is started and I can assert that my client can retrieve some things from the service.

    Private Blah proxy;

    [SetUp]
    public void SetUp()
    {
         proxy= new Blah();
    }

    [Test]
    public void GetStuff()
    {
        var result = proxy.GetStuff();
        Assert.NotNull(result);
    }

This doesn't work because my service isn't running. How would I make start my service before the tests?

役に立ちましたか?

解決

For integration/acceptance testing of WCF services I suggest you to use Self Hosted WCF Service. Examples you can find here:

Create self-hosted service on fixture setup, and close it on fixture tear down:

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();
}

With service hosted you can get service proxy:

var binding = new BasicHttpBinding();
ChannelFactory<IService1> factory = 
     new ChannelFactory<IService1>(binding, address);
service = factory.CreateChannel();

And your test will look like:

[Test]
public void ShouldReturnSomeStuff()
{
    var result = service.GetStuff();
    Assert.NotNull(result);
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top