This is a mirror of official site: http://jasper-net.blogspot.com/

How to Create Better Unit-Testing with Microsoft Moles Framework

| Thursday, September 1, 2011
Introduction

Unit testing is something that we all do, even if we don't really use any framework for that such as MS Test or NUnit. If we're writing class library we usually create another console application project to test the methods and the behavior of the new classes.

You can read my previous post about writing TDD and unit tests in Microsoft Visual Studio 2010 here.

One of the goals of unit testing is isolation. When we unit test a method, we usually want to check that it behaves as accepted, without any other objects to interfere. For example, let's take a very simple scenario:

pic1.png

We want to test GetInfo method (in Manager Class inside our BL) that calls GetFileContent method in DAL library.

BL GetInfo Implementation

pic2.png

When we test GetInfo, we want to check that it returns a valid result according to our inputs, but we also want to achieve isolation – meaning, we don’t want to be depend on ResouceReader object. We don't want our test to pass or fail due to GetFileContent result. How can we achieve this? How can we achieve isolation?

Before Moles Framework

In order to detour DAL.ResourceReader.GetFileContent method, we need to somehow replace the real DAL object (m_dal) in the BL.Manager object to a fake one that its implementation of GetFileContent is no more than 1 safe line… By saying "safe" I mean that it won't do anything "dangerous" such as reading from a resource that can\might fail. Because our ResourceReader class is implementing an interface (IResourceReader), we can create a new class in the test project that implements IResourceReader and its GetFileContent. Then we can use Private Accessor of the Manager object to set m_dal to our fake object.


Read more: Roy Rosenberg Part 1, Part 2
QR: how-to-create-better-unit-testing-with-microsoft-moles-framework-part-i.aspx

Posted via email from Jasper-net

0 comments: