Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Unit Test .NET Business Objects Using Nmock Library

Nmock is a unique way to test complex business objects in your development environment.


advertisement

Writing unit script code for your complex business objects in a test-driven development environment really gets difficult, because business classes may have too many external dependencies (high coupling) on other objects.

It sometimes won't be possible for limited-budget projects to set up development environments appropriately and configure them for unit testing. Developers can overcome these issues by using mock test objects.

[login] By creating mock business objects (without relying on real implementations of your complex business objects) you can unit test a single class at a time. Mock objects are simulated objects that replicate the behavior of your business objects, and you can use these mock objects in your actual unit test script code.

Using Nmock Library



There are few Microsoft .NET-supported mock testing frameworks available -- Nmock is one of them. Nmock can also be easily integrated with Nunit testing framework. Nmock is free, and 2.0 is the latest version that works with both .NET framework versions 1.1 and 2.0. You can download it from this Nmock site.

Nmock is a unique mock testing library with the following characteristics:

      * Like any other unit testing framework, developers need to define their expectations. What you are expecting as output?

* Mock objects are dynamic in nature and generated on the fly, based on implemented interfaces.

* Your test case will fail if the actual output doesn't match up with the expected output.

* Error messages show detailed descriptions of the error.

In this article I will show you an example of how to use Nmock library with .NET framework version 2.0.

Setup the Nmock Testing Environment

To develop the sample application, I used visual studio 2008 with .NET framework 2.0. For unit testing, I used Nunit version 2.5.4, freely downloadable from the Nunit site. But in the sample project I have only referenced NUnit.Framework.dll.

I downloaded the Nmock library from the site listed above. Once you unzip it, you'll get a few dll files. I have only added the reference of NMock2.dll in this sample project -- that's the latest one. This sample application displays common length- and distance-conversion factors while converting one type of length measurement unit to another. For example, the conversion factor value for converting meter to feet is 3.28.

First, in the consol-type C# project, I have created IconversionService interface having the GetConversionRate method. This method takes "from-length" and "to-length" type string parameters and returns the conversion factor (double data type). I have referred to this IconversionService interface in my mock test script.

using System;
using System.Collections.Generic;
using System.Text;

namespace MockTesting.Sample
    {
    public interface IconversionService
        {
        double GetConversionRate ( string fromlength, string tolength );
        }

    }

The ConversionServices class implements the IconversionService interface; the overridden GetConversionRate function is the only method of this class. While converting meters to feet, or mile to kilometer, I have called the GetConversionRate method.

using System;
using System.Collections.Generic;
using System.Text;

namespace MockTesting.Sample 
    {
    public class ConversionServices : IconversionService
        {
    
       public double GetConversionRate ( string fromlength, string tolength )
            {
            if ( fromlength.Length <=0 || tolength.Length <= 0 )
                return 0;

              switch(fromlength)       
              {         
                 case "MET":   
                    if (tolength.Trim().ToString() == "FT" )
                        return 3.28;
                    break;                  
                 case "FT":            
                       if (tolength.Trim().ToString() == "MET" )
                           return 0.3048;
                     break;

                 case "MILE":
                     if ( tolength.Trim ( ).ToString ( ) == "KM" )
                         return 1.609344;
                     break;

                 case "KM":
                     if ( tolength.Trim ( ).ToString ( ) == "MILE" )
                         return 0.621371192;
                     break;
                 default:            
                    return 0;         
                          
               }
              return 0;
            }
       
        }
    }

For calculating the total converted value, for example converting 10 meters to feet, I have created the ConvertTotalValue class. ConvertTotal function of this class calculates the total converted value. If you look at this function code you will see that the GetConversionRate method of IconversionService interface is called for length conversion. I have also created a read-only instance of the IconversionService interface in ConvertTotalValue class and instantiated that in the constructor. This instance will act as a mock.

using System;
using System.Collections.Generic;
using System.Text;

namespace MockTesting.Sample
    {
    public class ConvertTotalValue
        {
        private readonly IconversionService conversionService;

        public double ConvertTotal ( string fromlength, string tolength, double totvalue )
            {
            if ( fromlength.Trim ( ).Length <=0 || tolength.Trim ( ).Length <=0 )
                return 0;
            if ( totvalue == 0 )
                return 0;
            return conversionService.GetConversionRate ( fromlength, tolength ) * totvalue;
            }

        public ConvertTotalValue ( IconversionService conversionService )
            {
                this.conversionService = conversionService;
            }
        }
    }


Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap