23

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!

w00
  • 26,172
  • 30
  • 101
  • 147

2 Answers2

21

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.

Community
  • 1
  • 1
Channs
  • 2,091
  • 1
  • 15
  • 20
  • so other than adding a service reference into lets say a new C# project which will generate a client proxy for you, what's the difference of doing that vs. using the svcutil.exe. Why would you want to use it vs. just adding a reference to your project which will do the same thing right? or maybe not. I'm trying to understand more about client proxies in terms of testing them in WCF – PositiveGuy Oct 21 '13 at 03:15
  • @CoffeeAddict - Both approaches (project reference or svcutil) work perfectly. In the former approach, Visual Studio does some "behind the scenes magic" and generates a bunch of files, this is easier to update. However, in the latter approach, you deal with one proxy file, but requires you to run the command again for an update. I prefer the latter, since I like commiting one proxy file to my source control and I don't mind the update process via command line. Makes sense? – Channs Oct 21 '13 at 06:46
3

You can generate you proxy using the svcutil.exe (from Visula Studio: Add a Service Reference...). This will generate your client proxy and a Service Interface.

For exemple for your service http:://somesite.com/wcf/RealService.svc?wsdl we will get:

  • IRealService (interface)
  • RealServiceClient (implements IRealService and extends System.ServiceModel.ClientBase)

You can create a mock class which implements your service interface (IRealService).

And in your application instead of instanciating the concrete service client (RealServiceClient) when you want to call your service you can use a Factory or an IOC container.

This way you can decide which instance your application (or modules/components) works with: real service in runtime, mock when testing.

polkduran
  • 2,533
  • 24
  • 34
  • I have seen someone just add their WCF project reference into their C# test project and not even need to add a service reference to their C# project, is that possible or right to do? Also, isn't the point of mocking not to use the service instance at all, even if you're injecting it? It's supposed to be isolated, so doesn't the mocking framework create a fake service instance for you or something like that? – PositiveGuy Oct 21 '13 at 03:29
  • When you call the **Add a Service Reference...** command you will get some generated code that you can use as *regular code*. I mean, you can reference the assembly where the **proxy classes** are generated and use them without the need to do call the **Add a Service Reference...** command again (in a test project for instance). The *problem* is that the contract (`interfaces`) and their implementation (`classes`) are generated in the same file (thus the same assembly), but nothing stops you from creating a `mock` implementation in your test project. – polkduran Oct 21 '13 at 11:23
  • As long as your main application handles `interfaces` instead of the concrete implementation you can decide to **inject** the *real* service implementation or a *mock* class, for example when you are testing or you don't want to call the *real* services (when prototyping for instance). That is the point of *mocking*. – polkduran Oct 21 '13 at 11:30