Translate

Sunday, August 7, 2011

Assert and Descriptions,in Implementing Rhino Mocks Using Visual Studio

Ordered and Unordered

This helps in how the methods are being called,you can specify ordered or Unordered
Ordered--> it will check for same order which you specify in you mock test case with your actual method calling methods and objects
UnOrdered:->you no need to specify the order of the methods in mock test and it will will execute by default order

Expect.Call

Expect call will check for specified method is being called for example the test case public void GetStudentMockTest() in my earlier post i declared
Expect.Call(mockStudentRepository.GetStudentDetails()).Return(student1).IgnoreArguments();
here i am asking my test case to see in my service method it calls the GetStudentDetails() method,
It will return me student1 object


How to use for Call Repetition

Repeat.Any
Repeat.Any is mainly every important when you have test case where you are using foreach
for example let us say you have method call insertnew student record
and you have service where you are inserting multiple records at one time

public string submitStudnetRecors(List students)

{
foreach(studnet in students)
{
StudentBal student=new StudentBal();
studentbal.InsertNewstudent((studnet)


}
}

In this your repository would be like this

Expect.Call(mockStudentRepository.InsertNewstudent((studnet)).Repeat.Any.Return(message)

and other
Repeat.Once() ==> the default
Repeat.Any() ==> for number of times
Repeat.Never()
Repeat.AtLeastOnce()
Repeat.Twice()
Repeat.Times(13)
Repeat.Times(2, int.MaxValue)

Mock test with Void Methods

Sometimes we may also face situation where we want to do a mock test for void methods
example:
public void sendEmail(emailDO)
{
your code here;
}
in this case you need to declare like this
Expect.Call(() => mailRepository.sendEmail(emailDO))

Ignore Argument Constraints

Expect.Call(mockRepository.GetEmployeeDetails(EmployeeId)).Return(EmployeeObject) IgnoreArguments()

Here IgnoreArguments() is the keyword used here it will just ignore any arguments we passed ,and it just return whatever we mentioned in the Return, example here it just returns the EmployeeObject Return(EmployeeObject)

Some More Useful Assert statements

Is.Anything()
Is.Equal(3) ,Is.NotEqual(42) (this is applicable even for objects but need to write equal override for the object
)
Is.Same(obj) for objects
Is.NotSame(obj) ) for objects
Is.Null() it Checks for NULL values
Is.NotNull()
Is.GreaterThan(3)
Is.GreaterThanOrEqual(3)
Is.LessThan(3)
Is.LessThanOrEqual(3)

Text.StartsWith("hi Rhino Mock");
Text.EndsWith("Good Bye");
Text.Contains("Sachin Tendulkar");


For Exception Type Testing for Rhino Mock Test cases

[Test]
[ExpectedException(typeof(BusinessException), ExpectedMessage = "Invalid EmployeeId")]
public void Update_Employee_will_Return_Excception_If_The_EmployeeId_Is_Less_than_OR_equalToZero()
{
//Test Case code here where exception occurs and will check for exception type and message if any
}

No comments:

Post a Comment