What is mocking? Well....according to Wikipedia it is:
"In object-oriented programming, mock objects are simulated objects that mimic the behavior of real objects in controlled ways. A computer programmer typically creates a mock object to test the behavior of some other object"
In short, Mock objects are a great way to simulate an object that we need to work with. Sometimes we may also come across situations or states that are difficult to reproduce. Say for example we need to simulate a network error, or a specific error in our DB, with a mocking framework we can easily do this. Another great thing about mocking frameworks is whatever external data is being used won't be affected. We can simulate a DB call and not affect our DB at all. You can also write a mock for a piece of code that doesnt exist.
Why use Mocks
When writing unit tests, we only need to write tests that cover a specific area of code. The code under test or class under test may interact with other classes or external systems, but we only need to isolate the class that we are working with. Enter a mocking framework, this makes it a lot easier for us to re-use our mock objects and write great unit tests.
When I first started to get into TDD, it took me a while to get my head around mocking. Personally I feel that Moq is a really easy framework to use. That said, there are loads of other frameworks out there . For this example (and also because I have only worked with this framework), I'm going to use the Moq framework
Moq is an open source framework and has adopted an API that tries to be both simple to learn and easy to use. The API also follows the arrange-act-assert style and relies heavily on .NET 3.5 features, such as lambdas and extension methods. The learning curve is quite easy, but you need to feel comfortable with using lambdas.
Let's say we need to mock the data layer for our example project.
First, lets set up our mock that we are going to use.
var _mock = new Mock<IBasketDal>();
Then we need to tell this mock what its going to do, and what it will return
_mock.Setup(x => x.GetCustomerTotal(It.IsAny<int>())).Returns(25.5);
In the above line of code, we are telling the framework to pass in any variable into the GetCustomerTotal method, and we are telling this method to return a value of 22. In this way, we never hit the database - our mock will intercept and tell the method to return the result that we chose. This way, we can test for certain scenarios easily. We don't have to mess with the data to get a certain circumstance to work, we just tell it what we want it to do! Simples!
We call our method and check that the result returned is correct.
DataAccess.Basket totals = _target.CalculateBasketTotals(1);
Assert.That(totals.Total, Is.EqualTo(25.5), "The basket totals did not add up correctly");
As you can see using a mocking framework is a great way to go about TDD. I hope this tutorial helped a bit with understanding the basics of writing mocks using Moq. It is a very basic introduction to Moq, but for more info please check out the website for more information.
Introduction to moq
Ive included a small demo project to go along with this post. In order to run this project, you will need to install nUnit and moq. You could also eaily adapt this to run with another testing framework.
Add your comment