2

I have to consume a number of ASMX webservices in my project but their method calls are making it hard to unit test to rest of my code.

When I've consumed a WCF service in the past it generates an interface for the client which allows me to mock the service calls in my unit tests.

Is the same thing possible for ASMX webservices?

SharpC
  • 6,974
  • 4
  • 45
  • 40
heymega
  • 9,215
  • 8
  • 42
  • 61
  • Yes. Can you show the code where you want to use the mock? What mocking framework do you use? – CodeCaster Oct 31 '14 at 12:32
  • Im using moq. I'll update my question with an example asap. So how can this be done? Is there a way of creating an interface from the web reference? – heymega Oct 31 '14 at 12:41
  • True but doesn't the methods need to be virtual so moq can override them? Webreference method aren't virtual :( – heymega Oct 31 '14 at 12:45
  • How do you generate the client? [Using svcutil you can generate a proxy with an interface](http://stackoverflow.com/questions/950150/how-to-use-a-wsdl-file-to-create-a-wcf-service-not-make-a-call). – CodeCaster Oct 31 '14 at 12:47
  • In VS > Add Service Reference > Advanced > Add Web Reference – heymega Oct 31 '14 at 12:49
  • @heymega: you can use "Add Service Reference" by itself. Don't use "Add Web Reference" – John Saunders Oct 31 '14 at 14:02
  • @JohnSaunders The service I'm consuming uses the older .NET 2.0 framework. I don't think you can target these via the Add Service Reference – heymega Oct 31 '14 at 14:08
  • Yes, you can. Keep in mind you can target even a Java service via Add Service Reference. And it creates it's own interfaces, which will stay up to date. – John Saunders Oct 31 '14 at 14:56
  • @JohnSaunders You're correct, I never knew that. It makes sense though since WCF and ASMX can both use the httpbinding. Thank you – heymega Oct 31 '14 at 16:23

2 Answers2

4

My Solution

  1. Add the web reference in Visual Studio by right-clicking on Service References > Add Service Reference > Advanced > Add Web Reference.
  2. Open up the reference.cs file that is generated
  3. Right-click on the class name of your service and choose Refactor > Extract > Extract Interface. This will give you an interface representing your service methods.
  4. Create a new partial class for your service and make it inherit from your new interface, making sure the namespace and name of your class match that of your service in reference.cs

Now you've got the interface, you'll be able to mock the service in your unit tests.

I hope this helps other people and if theres an easier method please let me know.

heymega
  • 9,215
  • 8
  • 42
  • 61
-2

You can use WcfTestClient which is install by default with Visual Studio. It is easy to use and suitable to test wcf and asmx service.

You can find this tool from the path "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\WcfTestClient.exe"

You can do unit testing as well by Adding a service reference of asmx service in you project. when you open Reference.cs you will get an interface [SeriveName]Soap.cs which contain all the methods for unit test .

Community
  • 1
  • 1
  • Thank you for your answer however I'm not trying to test the service instead im trying to decouple its dependency in my code so I can unit test. – heymega Oct 31 '14 at 13:46