C# – Testing a WCF web service

cvisual studio 2010wcfweb services

I wanted to create a test class for a WCF service. I believe "mocking" is the right term for this?

I'm not really sure that the way i think i have to do this is the correct way. I have been given a URL to a WCF service, for example:

http:://somesite.com/wcf/RealService.svc

And:

http:://somesite.com/wcf/RealService.svc?wsdl

So instead of actually adding the RealService.svc to my project as Service Reference i simply added a new empty WCF Service to my project called Service1.

I then want to use the wsdl.exe (or maybe the svcutil.exe?) tool to generate an interface from the WSDL url: http:://somesite.com/wcf/RealService.svc?wsdl.

I then open the Service1.cs file and instead of letting is inherit from IService1.cs i let it inherit from the generated interface.

Then instead of calling the real service in my application i simply call my Service1 class. Is that how mocking a web service works..?

Also need to figure out how to actually generate an interface with the svcutil tool (i've read that i can't use wsdl.exe for a WCF service?). So any tips on that are more than welcome aswell!

Best Answer

Many areas to touch upon, will attempt to point you in the right directions:

  • If you want to test (i.e. pass input, verify output) your WCF service, use the Visual Studio GUI tool WCF Test Client (MSDN article here).

  • If you want to mock your WCF service (i.e. unit test your component which consumes the WCF service), use a mocking framework like NMock2 which allows you to mock the Service Interface (related SO thread here). You could hand-code a mock too (by implementing the interface), if you don't want to use an external framework - but this is more involved.

  • If you want to unit test your WCF service (i.e. write unit tests for service, business, data, etc.), use a popular mocking framework (related SO thread here).

  • To generate a proxy for your WCF service, use the svcutil.exe command line utility (MSDN article here) as you guessed. This utility comes with various options (language, namespace, config file, etc.), so pay attention to them.

Hope this helps.

Related Topic