मैं परीक्षण वेब सेवाओं, विशेष रूप से WCF क्लाइंट और सर्वर, पाया है निम्न परिदृश्यों में नियमित रूप से इकाई परीक्षण के शीर्ष पर उपयोगी:
- स्वीकृति परीक्षण जहाँ आप अपने पूरे सेवा का परीक्षण और चीजों प्रहार ब्लैक बॉक्स करना चाहते हैं चरम पर।
- एक विशिष्ट डब्ल्यूसीएफ वायर अप, एक्सटेंशन, व्यवहार इत्यादि का परीक्षण
- परीक्षण करना कि आपका इंटरफ़ेस और आपके डेटा सदस्य सही तरीके से सेटअप हैं।
अधिकांश समय मैं मूल http के साथ एक बहुत ही बुनियादी सेटअप का उपयोग करने की कोशिश करता हूं और कोड में सब कुछ तार करता हूं। जब तक मैं एकीकरण या स्वीकृति परीक्षण नहीं करता हूं, मैं सर्वर के खिलाफ क्लाइंट का परीक्षण नहीं करता हूं, इसके बजाय मैं उनमें से एक को नकल करता हूं ताकि मैं अलगाव में दूसरे का परीक्षण कर सकूं। नीचे कैसे मैं WCF ग्राहकों और सेवाओं का परीक्षण के उदाहरण हैं:
public static ServiceHost CreateServiceHost<TServiceToHost>(TServiceToHost serviceToHost, Uri baseAddress, string endpointAddress)
{
var serviceHost = new ServiceHost(serviceToHost, new[] { baseAddress });
serviceHost.Description.Behaviors.Find<ServiceDebugBehavior>().IncludeExceptionDetailInFaults = true;
serviceHost.Description.Behaviors.Find<ServiceBehaviorAttribute>().InstanceContextMode = InstanceContextMode.Single;
serviceHost.AddServiceEndpoint(typeof(TServiceToHost), new BasicHttpBinding(), endpointAddress);
return serviceHost;
}
//Testing Service
[TestFixture]
class TestService
{
private ServiceHost myServiceUnderTestHost;
private ChannelFactory<IMyServiceUnderTest> myServiceUnderTestProxyFactory;
[SetUp]
public void SetUp()
{
IMyServiceUnderTest myServiceUnderTest = new MyServiceUnderTest();
myServiceUnderTestHost = CreateServiceHost<IMyServiceUnderTest>(myServiceUnderTest, new Uri("http://localhost:12345"), "ServiceEndPoint");
myServiceUnderTestHost.Open();
myServiceUnderTestProxyFactory = new ChannelFactory<IMyServiceUnderTest>(new BasicHttpBinding(), new EndpointAddress("http://localhost:12345/ServiceEndPoint"));
}
[TearDown]
public void TearDown()
{
myServiceUnderTestProxyFactory.Close();
myServiceUnderTestHost.Close();
}
[Test]
public void SomeTest()
{
IMyServiceUnderTest serviceProxy = myServiceUnderTestProxyFactory.CreateChannel();
serviceProxy.SomeMethodCall();
}
}
//Testing Client
[TestFixture]
class TestService
{
private ServiceHost myMockedServiceUnderTestHost;
private IMyServiceUnderTest myMockedServiceUnderTest;
[SetUp]
public void SetUp()
{
myMockedServiceUnderTest = Substitute.For<IMyServiceUnderTest>(); //Using nsubstitute
myServiceUnderTestHost = CreateServiceHost<IMyServiceUnderTest>(myMockedServiceUnderTest, new Uri("http://localhost:12345"), "ServiceEndPoint");
myServiceUnderTestHost.Open();
}
[TearDown]
public void TearDown()
{
myServiceUnderTestHost.Close();
}
[Test]
public void SomeTest()
{
//Create client and invoke methods that will call service
//Will need some way of configuring the binding
var client = new myClientUnderTest();
client.DoWork();
//Assert that method was called on the server
myMockedServiceUnderTest.Recieved().SomeMethodCall();
}
}
नोट
मैं उल्लेख करना भूल गया था कि अगर आप कुछ भी तो महल गतिशील प्रॉक्सी का उपयोग करता है का उपयोग कर एक WCF सेवा नकली करना चाहते हैं तो आप करेंगे ServiceContractAttribute
को नकली में कॉपी करने से रोकने की आवश्यकता है। मेरे पास blog post है लेकिन मूल रूप से आप नकली बनाने से पहले प्रतिकृति से बचने के लिए विशेषता को एक के रूप में पंजीकृत करते हैं।
Castle.DynamicProxy.Generators.AttributesToAvoidReplicating
.Add<ServiceContractAttribute>();