Telerik blogs

In this post, i will be digging in a bit deep on Mock.Assert. This is the continuation from previous post and covers up the ways you can use assert for your mock expectations. I have used another traditional sample of Talisker that has a warehouse [Collaborator] and an order class [SUT] that will call upon the warehouse to see the stock and fill it up with items.

Our sample, interface of warehouse and order looks similar to :

  1. public interface IWarehouse
  2. {
  3.     bool HasInventory(string productName, int quantity);
  4.     void Remove(string productName, int quantity);
  5. }
  6.  
  7. public class Order
  8. {
  9.     public string ProductName { get; private set; }
  10.     public int Quantity { get; private set; }
  11.     public bool IsFilled { get; private set; }
  12.  
  13.     public Order(string productName, int quantity)
  14.     {
  15.         this.ProductName = productName;
  16.         this.Quantity = quantity;
  17.     }
  18.  
  19.     public void Fill(IWarehouse warehouse)
  20.     {
  21.         if (warehouse.HasInventory(ProductName, Quantity))
  22.         {
  23.             warehouse.Remove(ProductName, Quantity);
  24.             IsFilled = true;
  25.         }
  26.     }
  27.  
  28. }

 

Our first example deals with mock object assertion [my take] / assert all scenario. This will only act on the setups that has this “MustBeCalled” flag associated. To be more specific , lets first consider the following test code:

  1.    var order = new Order(TALISKER, 0);
  2.    var wareHouse = Mock.Create<IWarehouse>();
  3.  
  4.    Mock.Arrange(() => wareHouse.HasInventory(Arg.Any<string>(), 0)).Returns(true).MustBeCalled();
  5.    Mock.Arrange(() => wareHouse.Remove(Arg.Any<string>(), 0)).Throws(new InvalidOperationException()).MustBeCalled();
  6.    Mock.Arrange(() => wareHouse.Remove(Arg.Any<string>(), 100)).Throws(new InvalidOperationException());
  7.  
  8.    //exercise
  9.    Assert.Throws<InvalidOperationException>(() => order.Fill(wareHouse));
  10.    // it will assert first and second setup.
  11.    Mock.Assert(wareHouse);

Here, we have created the order object, created the mock of IWarehouse , then I setup our HasInventory and Remove calls of IWarehouse with my expected, which is called by the order.Fill internally. Now both of these setups are marked as “MustBeCalled”. There is one additional IWarehouse.Remove that is invalid and is not marked.   On line 9 ,  as we do order.Fill , the first and second setups will be invoked internally where the third one is left  un-invoked. Here, Mock.Assert will pass successfully as  both of the required ones are called as expected. But, if we marked the third one as must then it would fail with an  proper exception. Here, we can also see that I have used the same call for two different setups, this feature is called sequential mocking and will be covered later on.

Moving forward, let’s say, we don’t want this must call, when we want to do it specifically with lamda. For that let’s consider the following code:

  1. //setup - data
  2. var order = new Order(TALISKER, 50);
  3. var wareHouse = Mock.Create<IWarehouse>();
  4.  
  5. Mock.Arrange(() => wareHouse.HasInventory(TALISKER, 50)).Returns(true);
  6.  
  7. //exercise
  8. order.Fill(wareHouse);
  9.  
  10. //verify state
  11. Assert.True(order.IsFilled);
  12. //verify interaction
  13. Mock.Assert(()=> wareHouse.HasInventory(TALISKER, 50));

 

Here, the snippet shows a case for successful order, i haven’t used “MustBeCalled” rather i used lamda specifically to assert the call that I have made, which is more justified for the cases where we exactly know the user code will behave. But, here goes a question that how we are going assert a mock call if we don’t know what item a user code may request for. In that case, we can combine the matchers with our assert calls like we do it for arrange:

  1. //setup - data
  2.  var order = new Order(TALISKER, 50);
  3.  var wareHouse = Mock.Create<IWarehouse>();
  4.  
  5.  Mock.Arrange(() => wareHouse.HasInventory(TALISKER, Arg.Matches<int>( x => x <= 50))).Returns(true);
  6.  
  7.  //exercise
  8.  order.Fill(wareHouse);
  9.  
  10.  //verify state
  11.  Assert.True(order.IsFilled);
  12.  
  13.  //verify interaction
  14.  Mock.Assert(() => wareHouse.HasInventory(Arg.Any<string>(), Arg.Matches<int>(x => x <= 50)));

 

 

 

 

 

 

Here, i have asserted a mock call for which i don’t know the item name,  but i know that number of items that user will request is less than 50.  This kind of expression based assertion is now possible with JustMock. We can extent this sample for properties as well, which will be covered shortly [in other posts].

In addition to just simple assertion, we can also use filters to limit to times a call has occurred or if ever occurred. Like for the first test code, we have one setup that is never invoked. For such, it is always valid to use the following assert call:

  1. Mock.Assert(() => wareHouse.Remove(Arg.Any<string>(), 100), Occurs.Never());

Or ,for warehouse.HasInventory we can do the following:

  1. Mock.Assert(() => wareHouse.HasInventory(Arg.Any<string>(), 0), Occurs.Once());

Or,  to be more specific, it’s even better with:

  1. Mock.Assert(() => wareHouse.HasInventory(Arg.Any<string>(), 0), Occurs.Exactly(1));

 

There are other filters  that you can apply here using AtMost, AtLeast and AtLeastOnce but I left those to the readers. You can try the above sample that is provided in the examples shipped with JustMock.Please, do check it out and feel free to ping me for any issues.

 

Enjoy!!


Comments

Comments are disabled in preview mode.