Then define hello class with a Greet method, which greets hello followed by the IPerson ' s Name.
public class Hello
{
IPerson person;
Public Hello (IPerson person)
{
This.person = person;
}
Public String Greet ()
{
return ' Hello ' + person. Name;
}
}
As we can see here, Hello class have a dependency on ipersonbefore we go no further with the Hello class, let's learn some Basics about NMock. In NMock It's really easy-to-create a mock object based on a given interface or class. It is a three steps process where, first you instantiate a Mock object by passing in the type of the interface or class th At your is trying to mock. Then your would record the behaviour of the mock object and finally you get a working instance of the mock type through the Properties of the Mock object. Here's an example of what we create a mock instance in the simplest form without recording all behaviour based on the IPer Son interface.
Tell NMock which interfaces or classes is mocking Imock mockperson
= new Dynamicmock (typeof (IPerson));
Get a mock instance of the specified type
iperson person = (IPerson) mockperson.mockinstance;
However normally a mock instance is isn't really useful unless we record the behaviour of what it's supposed to does or what To expect before it is used. In the following example, we record and setup the values for the IPerson's Name property:
Tell NMock which interfaces or classes is mocking Imock mockperson
= new Dynamicmock (typeof (IPerson));
Set up the values person
. Expectandreturn ("Name", "John Doe");
Get a mock instance of the specified type
iperson person = (IPerson) mockperson.mockinstance;
NMock has a long list of useful Expect methods, which can is used to setup the behaviours of the mock object such as when Method A is called return B, or as return B when method A was called with parameter C, or throw Exception E if method a I s called or even tell the mock object of it should not being expecting call to method A at all. What's done above are simply saying that we expect the Name of the property would be accessed once if it is acces Sed it would return the string "John Doe". Note that we expect the property Name to being called once only. We can verify this by calling the Verify () method on the mock object.
Verify expectations on the mocking objects person
. Verify ();
Here's a quick list of what's available for setting up expectations:
Expect (String methodName, object[] args)
Expectandreturn (String MethodName, Object ReturnVal, object[] args)
Expectandthrow (String methodName, Exception exceptionval, object[] args)
expectnocall (String methodName)
Now gathering all the basics, we can easily apply it with our test for Hello class.
So in our test:
[Testfixture]
public class hellotest:assertion
{
[Test] public
void Testexpect ()
{
//Mock the dependency
Imock person = new Dynamicmock (typeof (IPerson));
Setting up values person
. Expectandreturn ("Name", "John Doe");
Hello hello = new Hello ((IPerson) person. mockinstance);
Assertequals ("Hello John Doe", hello.) Greet ());
Verify that Name property was only accessed once person
. Verify ();
}
}