Thinking in Mock

As James Kovacs concluded, NUnit is moke like a black box testing, while Mock is more like white box testing. Once I get used to NUnit’s assert style testing code, it took me hours to change my thoughts to adopt NMock. Right, it’s White Box! I need to write my expectation to run the test.

In Billy McCafferty’s Enterprise NHibernate and MVP example code, the ‘MockCustmerDao’ actually is a fake object, as James Kovacs explained. The proper mock way might be:

[Test]
public void TestInitViewUsingRealMock()
{
Mockery mockery = new Mockery();
IListCustomersView mockListCustomersView = mockery.NewMock<IListCustomersView>();
ICustomerDao mockCustomerDao = mockery.NewMock<ICustomerDao>();
List<Customer> mockCustomerList = new List<Customer>();
ListCustomersPresenter presenter = new ListCustomersPresenter(mockListCustomersView, mockCustomerDao);

// Expectation.
string message = “Click a customer’s ID to edit the customer.”;
Expect.Once.On(mockListCustomersView).SetProperty(“Message”).To(message);
Expect.Once.On(mockCustomerDao).Method(“GetAll”).Will(Return.Value(mockCustomerList));
Expect.Once.On(mockListCustomersView).SetProperty(“Customers”).To(mockCustomerList);

// Run action.
presenter.InitViewFor(“”);

mockery.VerifyAllExpectationsHaveBeenMet();
}

I couldn’t write expectation to presenter.DisplayMessagefor(“”) because it’s a private method, instead I used

Expect.Once.On(mockListCustomersView).SetProperty(“Message”).To(message);

How to expect a private method in an non-mocked object to be called?Maybe I should not even try to test any private method?

In RhinoMocks styel:

[Test]
public void TestInitViewUsingRhinoMocks()
{
MockRepository mockery = new MockRepository();

IListCustomersView mockListCustomersView = mockery.CreateMock<IListCustomersView>();
ICustomerDao mockCustomerDao = mockery.CreateMock<ICustomerDao>();
List<Customer> mockCustomerList = new List<Customer>();
ListCustomersPresenter presenter = new ListCustomersPresenter(mockListCustomersView, mockCustomerDao);

string message = “Click a customer’s ID to edit the customer.”;
Expect.Call( mockCustomerDao.GetAll()).Return( mockCustomerList );
mockListCustomersView.Customers = mockCustomerList;
mockListCustomersView.Message = message;
mockery.ReplayAll();

presenter.InitViewFor(“”);

mockery.VerifyAll();
}

And it’s much simpler in RhinoMocks’ new syntax:

[Test]
public void TestInitViewUsingRhinoMocksRecordStyleCode()
{
MockRepository mockery = new MockRepository();

IListCustomersView mockListCustomersView = mockery.CreateMock<IListCustomersView>();
ICustomerDao mockCustomerDao = mockery.CreateMock<ICustomerDao>();
List<Customer> mockCustomerList = new List<Customer>();
ListCustomersPresenter presenter = new ListCustomersPresenter(mockListCustomersView, mockCustomerDao);

using (mockery.Record())
{
string message = “Click a customer’s ID to edit the customer.”;
Expect.Call(mockCustomerDao.GetAll()).Return(mockCustomerList);
mockListCustomersView.Customers = mockCustomerList;
mockListCustomersView.Message = message;
}

//mockery.RePlayAll();

using (mockery.Playback())
{
presenter.InitViewFor(“”);
}

// ReplayAll() and VerifyAll() are implicitly called in new syntax.
//mockery.VerifyAll();
}

NMock’s tutorial is very easy to follow, from RhinoMock’s doc I can feel that RhinoMock is more complex and powerful, besides STRONG TYPE, in RhinoMock we can expect method come in ordered, method options (lastcall), etc.

Note: Expect.Call() for methods that has return values, and LastCall for methods that return void.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s