In object-oriented programming, mock objects are simulated objects that mimic the behavior of real objects in controlled ways. A developer creates a mock object to test the behavior of some other object, in much the same way that a car designer uses a crash test dummy to simulate the dynamic behavior of a human in vehicle impacts.
We use Mock object for NUnit testing if we have 2, 3 or more layer architecture.
In programming terms mock objects are objects that “imitate” some other objects, claiming to have the same attributes, methods etc. like the original.

Rhino Mocks allows you to easily create mock objects and setup a wide range of expectations on them using strongly typed notation instead of compiler-opaque strings.
To mock an object, its class MUST implement an interface.
Public class Car : ICar
{
public int StartEngine();
…
// More methods to be implemented
}
MockRepository mockRepository = new MockRepository();
MyCar = mockRepository.CreateMock<ICar>();
Here is the scenario and source code in C# for demonstration.
We have one solution and 2 projects, representing 2 layers.
Project: DataLayer which gets some information from xml file, database or some other web service, …
DataLayer project has one class: PersonDAL.
using System;
using System.Collections.Generic;
using System.Text;
namespace DataLayer
{
public class PersonDAL
{
private string _name;
private string _id;
private List
lista = new List
();
public PersonDAL(string name, string id)
{
_name = name;
_id = id;
}
public List
CreateList()
{
lista.Add(new PersonDAL("Peter", "1"));
lista.Add(new PersonDAL("George", "2"));
lista.Add(new PersonDAL("Michael", "3"));
return lista;
}
public PersonDAL GetPersonByID(string id)
{
PersonDAL result = new PersonDAL("", "");
result = null;
foreach (PersonDAL person in lista)
{
if (person.ID == id)
{
result = person;
break;
}
}
return result;
}
public string ID
{
get { return _id; }
}
public string Name
{
get { return _name; }
}
}
}
it is a simple class which gets data from some resource. In this case, data are hardcoded but never mind.
The other project is BusinessLayer, it represents some business logic. It is Layer 2.
Person class:
using System;
using System.Collections.Generic;
using System.Text;
using DataLayer;
namespace BusinessLayer
{
public class Person
{
public string _id;
public string _name;
public Person(string id, string name)
{
_id = id;
_name = name;
}
}
}
interface IPersonService:
using System;
using System.Collections.Generic;
using System.Text;
namespace BusinessLayer
{
public interface IPersonService
{
List
GetPersons();
Person GetPersonByID(string id);
}
}
and PersonService class:
using System;
using System.Collections.Generic;
using System.Text;
namespace BusinessLayer
{
public class PersonService : IPersonService
{
DataLayer.PersonDAL person = new DataLayer.PersonDAL("", "");
public List
GetPersons()
{
List
list = new List
();
foreach (DataLayer.PersonDAL objPerson in person.CreateList())
{
list.Add(new Person(objPerson.ID, objPerson.Name));
}
return list;
}
public Person GetPersonByID(string id)
{
return new Person(person.GetPersonByID(id).ID,person.GetPersonByID(id).Name);
}
}
}
Ok, that is part of some huge application. Let’s code some Unit test here. Supose we do not have data resource at this moment and we can not test this with real data. We must fake it.
So, create new project, type of Class Library into this solution and call it: Test
Add references for both projects and references for NUnit.Framework and
Rhino.Mocks. This project for now has only one class. We use it for testing.
PersonServiceTest class:
using System;
using System.Collections.Generic;
using NUnit.Framework;
using Rhino.Mocks;
using BusinessLayer;
using DataLayer;
[TestFixture]
public class PersonServiceTest
{
[SetUp]
public void TestInit()
{
}
[Test]
public void Test()
{
MockRepository mockRepository = new MockRepository();
IPersonService personRepository = mockRepository.CreateMock();
Person mockPerson = new Person("1", "Milan");
Person objPerson;
Expect.Call(personRepository.GetPersonByID("1")).Return(mockPerson);
mockRepository.ReplayAll();
objPerson = personRepository.GetPersonByID("1");
Assert.AreEqual(objPerson._id, mockPerson._id);
}
}



Recent Comments