Wednesday, September 20, 2006

Mock Object for Testing

 
    Mock: an imitation, pretend, mimics somebody

    I have been using unit testing for some time now, but never really bothered about mock objects. Over time when projects grow, so do unit tests and then unit tests began to look more like integration tests. Objects would call other objects, which in turn calls others all the way to database. Hence after most of the tests, there is a needed to reset database. So to do this we would put some script/db calls in test’s setup and it all works fine like a well oiled machine. Sometimes when we see an occasional red in tests, we can fire up my debugger attach it to NUnit and find the problem.
    So what's the problem? Well unit tests are ‘unit’ tests not integration tests or uat. They should be testing only a unit of code not the whole end to end application. When we see red in NUnit we should be able to know which object’s which function is acting up from tests itself. Hence there is a need to isolate each object and test it separately; here is where Mock object comes in. Mock object is a runtime object which can imitate as some object in a controlled way. It also allows setting mock responses. With mock objects we would be testing only single unit and not whole application. 
   There are lot of frameworks which provides mock object functionality Rhino-mocks , EasyMock.Net, NMock, NUnit.Mocks TypeMock.Net etc, most of them offer similar functionalities
Here is a sample using Nunit.Mocks

using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using NUnit.Mocks;
namespace TestMockObjects
{
    public interface ISomeInterface
    {
       string SomeFunction();
    }

    [TestFixture]
    public class SomeTestClass
    {
        DynamicMock mockObject;
        ISomeInterface someObject;
        [SetUp]
        public void SetUp()
        {
            mockObject = new DynamicMock(typeof(ISomeInterface));
            someObject = (ISomeInterface)mockObject.MockInstance;
            mockObject.SetReturnValue("SomeFunction", "I am Mock");
        }

        [Test]
        public void TestMock()
        {
            Assert.IsTrue("I am Mock" == someObject.SomeFunction());
        }
   }
}

Saturday, September 16, 2006

Working with Directory Services using .Net

DirectoryEntry and DirectorySearcher are major classes and most of time only these two are needed to work with Directory Service. Use following steps to retrieve user list from Directory Service
  • Use DirectoryEntry and bind using a query. Domain user’s credential is required only when user account running this query is not a domain user.
    DirectoryEntry root = new DirectoryEntry("LDAP://<domainName>/CN=Users,DC=<DomainName>", "<DomainName>\\<domainUser>", "<Password>");
  • Create DirectorySearcher and pass the DirectoryEntry earlier binded to this object. We need this so that we can use a filter to get only users and not groups.
    DirectorySearcher searcher = new DirectorySearcher(root);
  • Set a filter.
    searcher.Filter = "(objectClass=user)";
  • Fire a query.
    SearchResultCollection userList = searcher.FindAll();
  • Retrive the user information from the result collection.
    foreach (SearchResult result in userList)
    {
    DirectoryEntry userEntry = result.GetDirectoryEntry();
    string loginID = (string)userEntry.InvokeGet("samAccountName");
    string userName = (string)userEntry.InvokeGet("displayName");
    string emailID = (string)userEntry.InvokeGet("mail");

    }