Rhino Mocks & NMocks 2

time to read 4 min | 760 words

I just read Roy's message about NMock2. Vaderpi talks about it in more details, it's look very nice.

Here is the original NMock test:

[Test]
public void ComputeTotal() {
  // Create the mock object for item1 and have it return a price of 100
  DynamicMock item1Mock = new DynamicMock(typeof(Item));
  item1Mock.Strict = true;
  item1Mock.ExpectAndReturn(\"Price\", 100);
  Item item1 = (Item) item1Mock.MockInstance;

  // Create the mock object for item2 and have it return a price of 50
  DynamicMock item2Mock = new DynamicMock(typeof(Item));
  item2Mock.Strict = true;
  item2Mock.ExpectAndReturn(\"Price\", 50);
  Item item2 = (Item) item2Mock.MockInstance;

  // Add the items to the cart
  ShoppingCart cart = new ShoppingCart();
  cart.Add(item1);
  cart.Add(item2);

  // Get the total and make sure it is 150 (100 + 50)
  int total = cart.ComputeTotal();
  Assert.AreEqual(150, total);

  // Verify that all the expects have been met
  item1Mock.Verify();
  item2Mock.Verify();
}

Here is the NMock2 version, it looks much clearer, if a bit long.

[Test]
public void ComputeTotal() {
  Mockery mock = new Mockery();
  // Create the mock object for item1 and have it return a price of 100
  Item item1 = (Item) mock.NewMock(typeof(Item));
  Expect.Once.On(item1).GetProperty(\"Price\").Will(Return.Value(100));

  // Create the mock object for item2 and have it return a price of 50
  Item item1 = (Item) mock.NewMock(typeof(Item));
  Expect.Once.On(item1).GetProperty(\"Price\").Will(Return.Value(50));

  // Add the items to the cart
  ShoppingCart cart = new ShoppingCart();
  cart.Add(item1);
  cart.Add(item2);

  // Get the total and make sure it is 150 (100 + 50)
  int total = cart.ComputeTotal();
  Assert.AreEqual(150, total);

  // Verify that all the expects have been met
  mock.VerifyAllExpectationsHaveBeenMet();
}

And here is Rhino Mocks version:

[Test]
public void ComputeTotal() { 
 //Create mock controls and mock objects
 MockControl control1 = MockControl.CreateControl(typeof(Item)); 
 MockControl control2 = MockControl.CreateControl(typeof(Item)); 
 Item item1 = (Item)control1.MockInstance, item2 = control2.MockInstance;
 
 //Setup return value and expected call 
 control1.ExpectAndReturn(item1.Price,50); 
 control2.ExpectAndReturn(item2.Price,100);
 
 //Prepare for replay 
 control1.Replay(); 
 control2.Replay();
 
 //Add items to cart 
 ShoppingCart cart = new ShoppingCart(); 
 cart.Add(item1); 
 cart.Add(item2);
 
 // Get total and verify that we got the correct result 
 int total = cart.ComputeTotal(); 
 Assert.AreEqual(150,total);
 
 //Verify that all expectations have been met 
 control1.Verify(); 
 control2.Verify();
}

All three version express the same intent. I already talked about why I don't like NMock very much. I didn't have a chance to use NMock2, so I can say if it has any shortcoming, but I* think that there might be a problem in expressing complex conditions.**

I'm going to write a code project article for Rhino.Mocks, so far I think that only 1 Frown person downloaded it.

* Am I spearding FUD? I'm not really objective, you know ;-)

** Rhino Mocks solution is to use callbacks and verify the method using Asserts.