Here’s a situation I ran into a while ago, and will no doubt run into many, many times in the future:
- I implement a web service (in .NET 2.0)
- I add a web reference in another project
- I make calls to the web service
- I want to be able to test those calls, and how the client will react to different results
Now, this is an ideal situation for mocks or stubs – except that the proxy code generated by Visual Studio (or rather, wsdl.exe) doesn’t include an interface, which makes it trickier to mock. (I can’t remember whether the methods generated are even virtual or not.) Fortunately, it does write it out as a partial class in VS2005, which makes life a lot easier.
I solved this problem with a home-grown tool, which went through the following phases as a pre-build step:
- Check whether the web reference proxy code (specified as a parameter to the tool) has already been modified. (I use a comment at the top of the file to indicate that it’s already been processed.) If it has, stop.
- Load the file line by line, using a regular expression (yes, I do use them sometimes) to spot method signature.
- Begin a new file with the “I’m modified” comment.
- Write out all the old code.
- Write out all the method signatures as a new interface.
- Write out an extra partial class declaration which just makes the existing class implement the interface.
None of this is too hard – and it could be done with separate files instead of rewriting the generated one, etc. However, I was very surprised when I didn’t find such a tool on the net. (To release one myself I’d probably rewrite it so as to be totally mine rather than my company’s.)
Is there a tool out there which everyone uses and I’ve mysteriously not heard of? (Unlikely.)
Does everyone else take a different approach entirely? (Quite possible.)
Do people just not test their web service calls? (Likely for many people, but not for true TDD-ers, surely.)