Ayende @ Rahien

Hi!
My name is Ayende Rahien
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:

ayende@ayende.com

+972 52-548-6969

@

Posts: 5,947 | Comments: 44,540

filter by tags archive

Rhino Mocks & NMocks 2


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.


Comments

Comment preview

Comments have been closed on this topic.

FUTURE POSTS

No future posts left, oh my!

RECENT SERIES

  1. RavenDB Sharding (2):
    21 May 2015 - Adding a new shard to an existing cluster, the easy way
  2. The RavenDB Comic Strip (2):
    20 May 2015 - Part II – a team in trouble!
  3. Challenge (45):
    28 Apr 2015 - What is the meaning of this change?
  4. Interview question (2):
    30 Mar 2015 - fix the index
  5. Excerpts from the RavenDB Performance team report (20):
    20 Feb 2015 - Optimizing Compare – The circle of life (a post-mortem)
View all series

RECENT COMMENTS

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats