2014-02-21 44 views
1

我想寫一個自動迴歸測試,我的服務開始了,我可以斷言我的客戶端可以從服務中檢索一些東西。爲客戶編寫一個wcf服務的迴歸測試

Private Blah proxy; 

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

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

這不起作用,因爲我的服務沒有運行。如何在測試前開始我的服務?

回答

3

對於WCF服務的集成/驗收測試,我建議您使用Self Hosted 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); 
} 
相關問題