Translate

Saturday, July 30, 2011

Implementing Rhino Mocks Using Visual Studio .Net

Part One


Implementing Rhino Mocks Using Visual Studio .Net


Working with Rhino Mocks Using Visual Studio .Net 2010



Today i woked around on Rhino Mocks,I feel like to post this on Blog ,Rhino Mocks are best
to implement mock testing for your code.Since Rhino mocks are available at free of cost and easy to implement

I divided this topic into two parts
Today i am posting part one

(Recently added part3)
Implementing Mock Test cases for Repository in WCF Service,Please read it if you have time to know more about Repository Test cases ,Click here

How to implement Repository Mock Test cases in WCF Service


Topics in Discussion

Introduction to Rhino Mock.

Why we need Rhino Mock.
State Versus Behavior verification
How to setup Rhino Mock for Nuint
Rhino Mock Test cases Examples

Rhino Mocks are the Mock object Framework used for Test-Driven Development, it is used to Mock the actual (or Real object ) to a fake object in Unit Testing

Why we need Rhino Mock.
The real object has nondeterministic behavior (it produces
unpredictable results, like a stock-market quote
feed.)
The real object is difcult to set up.
The real object has behavior that is hard to trigger (for
example, a network error).
The real object is slow.
The real object has (or is) a user interface.
The test needs to ask the real object about how it was
used (for example, a test might need to conrm that a
callback function was actually called).
The real object does not yet exist (a common problem
when interfacing with other teams or new hardware systems).

State versus Behavior Verification
A Mock Object Framework such as Rhino Mocks can be used to perform two very different types of unit tests. You can use Rhino Mocks to perform either state or behavior verification
When performing state-based verification, the final statement in your unit test is typically an assert statement that asserts that some condition is truefication
When performing behavior verification (an interaction test), on the other hand, you are interested in verifying that a set of objects behave and interact in a particular way.

How to Setup Rhino Mocks in Visual Studio Solution

Rhino Mock Set Up
Using mock objects, we can get around all of these problems.
The three key steps to using mock objects for testing are:
1. Use an interface to describe the object
2. Implement the interface for your code
3. Implement the interface in a mock object for testing

Rhino Mocks was created and it is maintained by Ayende Rahien (AKA Oren Eini). You can download Rhino Mocks from the following URL:
http://www.ayende.com/projects/rhino-mocks.aspx
http://builds.hibernatingrhinos.com/builds/Rhino-Mocks
There is a Google group devoted to discussing Rhino Mocks (Ayende Rahien is an active participant) at:
http://groups.google.com/group/RhinoMocks
Rhino Mocks does not include an installer. After you download and unzip Rhino Mocks, you can use Rhino Mocks in a project by adding a reference to the Rhino.Mocks.dll assembly. There is also an XML file included with the download that provides Intellisense for Rhino Mocks within Visual Studio.



I used two types of solutions one is using Layer architecture Solution


In this exmple need Nunit Tool to run the test cases

Example:

I Created Student information

1.Create a Sevice Layer Project
2.Create BAL Layer Project
3.Create DAL Layer Project
4.Create a Nunit Testing Project


Let us create a Repostiory BAL class


Object
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CommonObjects
{
public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
}
}

This will be an Interface
namespace BAL
{
public interface IStudent
{
CommonObjects.Student GetStudentDetails();
}
}

and
i am inheriting the interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonObjects;
namespace BAL
{
public class Student:IStudent
{
public CommonObjects.Student GetStudentDetails()
{
throw new NotImplementedException();
}
}
}

In this i just created one student Details method which will fetch student details let us say it is a

GetStudentDetails() and it has not implemented any code


Let us i want to using this method in Service
and service will look like this


Interface

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace Services.StudentService
{
// NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in App.config.
[ServiceContract]
public interface IService
{
[OperationContract]
List GetStudentList();
}

}


Service file

  1. using System.Collections.Generic;
  2. using BAL;
  3. using Services.StudentService;
  4. using Student = CommonObjects.Student;

  5. namespace Services.StudentService
  6. {
  7. public class Service : IService
  8. {
  9. private IStudent _student;
  10. public Service()
  11. {
  12. _student =new BAL.Student();
  13. }
  14. public Service(IStudent student)
  15. {
  16. _student = student;
  17. }
  18. public List GetStudentList()
  19. {
  20. List students = new List();
  21. CommonObjects.Student student = _student.GetStudentDetails();
  22. students.Add(student);
  23. return students;
  24. }

  25. }
  26. }

In the Above service i created two constructor

public Service()
Service(IStudent student)

This will help me to inject the mock class then call the actual method class
which will replace the mock repository

In the method name public List GetStudentList()

i am using getting the list of student using this method
CommonObjects.Student student = _student.GetStudentDetails();
and i am adding it to a list of students,Here GetStudentDetails() is still not implemented but still i can write a unit test case and can say please return me the list of students what i am expecting this method to return me,thats way the development will not have any dependency in writing the unit test case

The Test case will look like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonObjects;
using BAL;
using DAL;
using NUnit;
using NUnit.Framework;
using Rhino.Mocks;
using Services.StudentService;
using Student = CommonObjects.Student;

namespace DemoMockTest
{
[TestFixture, System.ComponentModel.Description("")]
public class StudentTest
{
private MockRepository _mockRepository;
private Service _studentService;
[TestFixtureTearDown]
public void TestFixtureTearDown()
{

}

[SetUp]
public void Initialize()
{

_studentService = new Service();
_mockRepository = new MockRepository();
}

[Test]
public void GetStudentMockTest()
{
CommonObjects.Student student1=new Student();
CommonObjects.Student student2 = new Student();
List students = new List();
students.Add(student1);
students.Add(student2);
var mockStudentRepository = _mockRepository.StrictMock();
_studentService = new Service(mockStudentRepository);
using (_mockRepository.Ordered())
{
Expect.Call(mockStudentRepository.GetStudentDetails()).Return(student1).IgnoreArguments();
}
_mockRepository.ReplayAll();
List resltedstudents = _studentService.GetStudentList();
_mockRepository.VerifyAll();
}

}
}


I am also including the solution of this exmple

In My next Blog i am going explain more about mock Repository test cases For More Information on Rhino Mocks on Part Two