background

Telerik JustMock

Assert Occurrence

  • JustMock allows you simple and fast assertion of how many times the method is called without the cost of developing and maintaining additional code.
  • Part of the fastest, most flexible and complete mocking tool for crafting unit tests.
  • Our award-winning support team is available to assist you with any issues.
Mocking and Assert Functionality
  • Assert Occurrence Overview

    In many situations, especially in complex projects with numerous features interacting with each other, a method can be called too many or too few times, resulting in unexpected behavior of the software. To verify that the method in question is called the exact number of times, additional code is typically written and maintained to handle that verification.

    JustMock allows you simple and fast assertion of how many times the method is called without the cost of developing and maintaining additional code.
  • Set Assertion Expectation During Arrange

    One of the possible options when verifying the occurrence of a method call is to set the expectation at the arrange phase and verify all assertion expectation for the mock object at the end of the test.
    [TestMethod]
    public void Test_ExecutionOfAddProduct_OccursOnce()
    {
        // Arrange
        Order order = Mock.Create<Order>(Behavior.CallOriginal);
        Mock.Arrange(() => order.AddProduct(Arg.IsAny<Product>(), Arg.AnyInt)).OccursOnce();
     
        // Action
        Product product = new Product("socks");
        order.AddProduct(product, 2);
     
        // Assert
        Mock.Assert(order);
    }
  • Assert the Occurrence Expectation at the End of the Test

    Another option is to directly assert that a specific method of a mock object is called certain times at the end of the test.
    [TestMethod]
    public void Test_ExecutionOfAddProduct_OccursOnce()
    {
        // Arrange
        Order order = Mock.Create<Order>(Behavior.CallOriginal);
     
        // Action
        Product product = new Product("socks");
        order.AddProduct(product, 2);
     
        // Assert
        Mock.Assert(() => order.AddProduct(product, 2), Occurs.Once());
    }

    Asserting occurrence documentation
Next Steps