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.)
The regular expression could take care of the “line by line” part as well. =]
LikeLike
Just start using WCF. Its proxy generator takes care of that issue.
LikeLike
I’m sure I’ll start using WCF at some point – but it won’t be for a little while.
Still, nice to know it handles it. (I really must get a couple of WCF books some time…)
Jon
LikeLike
Can’t you go into the non-regenerated part of the proxy class and do an Extract Interface from there?
LikeLike
I’m not sure what you mean by “the non-regenerated part” – but I’d rather not have to do anything manually, beyond explicitly updating the web reference. The interface should basically be the interface of the web reference – so updating the web reference should update the interface.
Jon
LikeLike
Jon,
You can define an interface for your web service, and decorate it with the Web Service attributes.
This lets you provide multiple implementations. Will that do what you want?
Gabe
LikeLike
Oops. See the link here:
http://blogs.msdn.com/kaevans/archive/2006/03/02/542111.aspx
LikeLike
I didn’t see anything on that page which suggested that an interface is generated for the web reference on the client side though – and that’s what I want to code to. That’s not to say it’s not useful, it’s just not what I was after :)
I suspect that a shared interface in a common assembly, combined with a manually written partial class declaration for the proxy class which states that it implements the interface is probably the best way to go, to be honest… I might look at moving the current codebase towards that and seeing how it pans out.
Jon
LikeLike
We typically write a thin wrapper class around the proxy and make that implement an interface we can mock.
An alternate solution would be to hook into the proxy generation process. I believe we did this on one app, but I don’t have the code handy to tell you more.
LikeLike
Adapter !!
Apparently, it appears you are rendering an adapter indirectly via the code generated by the tool
Raaj
LikeLike
We just did this on our project last week and like Jon said in a comment simply made the generated proxy code implement an interface via a small partial class. Fast, simple, easy, and best of all no regular exptrssions.
LikeLike
Yeah but we don’t always want proxy stubs do we.
i want a baby from across the wire and for it to have a client side cry methord and it’s fine for the service to have default properties but the generated stub does not refect these so when i make a baby client side it has no legs.
datasets come across the wire with methords and don’t need stubs so why not give us developers more freedom so we don’t need duplicates of a baby class.
WCF is a let down and is no better then web services.
LikeLike
Hi!
have a look at soapUI, a free web service testing tool that can generate and host a mock Web Service from any given WSDL in seconds.
Read more/download: http://www.soapui.org
Tutorial on mocking: http://www.soapui.org/gettingstarted/mocking.html
Good Luck!
regards,
/Ole
LikeLike