Dean Hume - Basic Introduction to writing unit tests with Moq - Part 3

added by deanomachino
11/29/2010 7:36:47 AM

0 Kicks, 107 Views

This post is the third part of a series on Mocking with Moq. If you haven't already taken a look at the first two posts on the basics of Moq, please check it out. Mocking a method within the same class So, recently I came across a problem where I had a method on a class that I needed to mock its return value. It had no interface and I wasn't sure if there was a way to mock it. Fortunately there is a way - and it's pretty easy to do! Let's take a look. In the previous articles that we have looked at, we wrote a few tests for a simple eCommerce Basket (Cart) system. In this example we are taking a look at a very simple method that calls another method to get the Geolocation details of an IP address. If you would like to know more about this Geolocation service - please check out the article on Scott Hanselman's blog. We don't want to call the 3rd party service every time we run our tests, so we need to mock out the return value. In this instance our method doesn't have a matching interface.