Can I Mock Web Services?

With Typemock Isolator, sure you can!

This came up in the forums. So here’s the explanation again, but with better graphics.

A web service is a great example for mocking dependencies. You have code that accesses the cloud. What if you’re working offline? Or, someone else is working on the service, and has not published it yet, while you’re stuck with the client code and no way to test it? Mocking to the rescue.

imageWhen you consume a web service, a class is generated for the service. The trick to find this proxy code is (thanks Roy!) to click “Show all files” when standing on the web reference. And you get something like this:

See the Reference.cs? That’s where the proxy code is created.
So let’s say we have the original wizard generated service. Here’s the code:

For this web method, the generated proxy looks like this:

Now that we have type and method we can reference, we can mock it. Here’s an example using reflective mocks:

And here’s one using Natural mocks:

There you go: simple yet powerful.