How UnitTest WCF.SVC Files

I plan on unit test WCF services that display as .svc files. What are the best methods available for unit test.

As I know, this is an available option:

Create a separate project and create proxy classes for .svc files and add them to the project and do unit testing on these proxy classes. Proxy classes can be created using:

  • svcutil.exe
  • "Add service link" for visual studio.

Is there any other better option for unit test of my wcf .svc file using nunit?

+4
source share
3 answers

Basically, you are not unit test services. You unit test their code, as if this code was not in the service.

Do not instantiate the service class. This is too much for a unit test. You would have unit test code inside this class. Remember that unit tests are designed to test the smallest possible units of code, not the entire service.

+3
source

If you need to test the service logic, you can write simple unit tests, as shown below:

 public class Service1 : IService1 { public string GetData(int value) { return string.Format("You entered: {0}", value); } } //Tests [TestFixture] public class MyService_Test { [Test] public void GetData_should_return_entered_string() { Service1 service = new Service1(); Assert.AreEqual("You entered: 1", service.GetData(1)); } } 

And if you want to test all the integration, you can write the following integration tests. In a nutshell, you need to run your service as a standalone and use _proxy to execute the service methods. Such tests are useful when you need to test extensibility points, such as a custom message inspector, error handlers, etc.

  private ITestService _proxy; private ServiceHost _host; [SetUp] public void Initialize() { const string baseAddress = "net.pipe://localhost/TestService"; _host = new ServiceHost(typeof(TestService), new Uri(baseAddress)); var factory = new ChannelFactory<ITestService>(new NetNamedPipeBinding(), new EndpointAddress(baseAddress)); _host.Open(); _proxy = factory.CreateChannel(); } 

References:

Integration Testing WCF Services

+2
source

If you just want to test the logic of the service, just instantiate the class that implements the logic and call its methods. It is very important to bypass WCF, which is good for unit tests, since there is no reason to check Microsoft code. Integration tests, on the other hand, are another matter.

+1
source

Source: https://habr.com/ru/post/1443592/


All Articles