OccursNever/OccursOnce not working properly

2 posts, 1 answers
  1. Javier
    Javier avatar
    10 posts
    Member since:
    Apr 2013

    Posted 07 May 2013 Link to this post

    Hi, I would like to test this static method:
    public static class UnitOfWorkExtensions
    {
     
            public static void SetSubCategory(this IOperationTracer tracer, string subCategory)
            {
                UnitOfWork unitOfWork = UnitOfWork.Current;
                if (unitOfWork != null && unitOfWork.CorrelationId != null)
                {
                    tracer.SetSubCategory(UnitOfWork.Current.CorrelationId, subCategory);
                }
            }
    }
    I am implementing the test like this:
    [TestMethod]
          public void SetSubCategoryTest()
          {
              Mock.SetupStatic(typeof(UnitOfWorkExtensions));
              IOperationTracer operationTracer = Mock.Create<OperationTracer>();
     
     
              Mock.Arrange(() => operationTracer.SetSubCategory(null, "subCategory")).DoNothing().OccursNever();
     
              UnitOfWorkExtensions.SetSubCategory(operationTracer, "subCategory");
              Mock.Assert(operationTracer);
     
              UnitOfWork.Current = new UnitOfWork();
              UnitOfWork.Current.CorrelationId = "11111";
     
              Mock.Arrange(() => operationTracer.SetSubCategory(null, "subCategory")).DoNothing().OccursOnce();
     
              UnitOfWorkExtensions.SetSubCategory(operationTracer, "subCategory");
              Mock.Assert(operationTracer);
     
          }
    The test is failing and I don't have a clue why
  2. Answer
    Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 07 May 2013 Link to this post

    Hi Javier,

    Thank you for contacting Telerik support.

    I managed to reproduce the issue. Debugging it, I noticed two things that might have caused it.
    1. To test the UnitOfWorkExtensions class, you don`t need to mock it in your current test method. However, if for any reason you will need this, you should specify the mock behavior to CallOriginal, like this:
      Mock.SetupStatic(typeof(UnitOfWorkExtensions), Behavior.CallOriginal);
      This will tell the UnitOfWorkExtensions class to call the original logic if there aren`t any further arrangements. Note that, in the refactored test below, I have completely dropped the mocking of this class.
    2. After you have specified the UnitOfWork.Current and its CorrelationId to "11111", you should expect a call to SetSubCategory with CorrelationId equals to "11111", instead of null. This is the main reason for the failing test. I wrote it like this and it behaves as expected:
      [TestMethod]
      public void SetSubCategoryTest()
      {
          IOperationTracer operationTracer = Mock.Create<OperationTracer>();
       
       
          Mock.Arrange(() => operationTracer.SetSubCategory(null, "subCategory")).DoNothing().OccursNever();
       
          UnitOfWorkExtensions.SetSubCategory(operationTracer, "subCategory");
          Mock.Assert(operationTracer);
       
          UnitOfWork.Current = new UnitOfWork();
          UnitOfWork.Current.CorrelationId = "11111";
       
          Mock.Arrange(() => operationTracer.SetSubCategory("11111", "subCategory")).DoNothing().OccursOnce();
       
          UnitOfWorkExtensions.SetSubCategory(operationTracer, "subCategory");
          Mock.Assert(operationTracer);
      Kaloyan
      Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  3. DevCraft R3 2016 release webinar banner
Back to Top