This is a migrated thread and some comments may be shown as answers.

OccursNever/OccursOnce not working properly

1 Answer 30 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Javier
Top achievements
Rank 2
Javier asked on 07 May 2013, 02:11 PM
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

1 Answer, 1 is accepted

Sort by
0
Accepted
Kaloyan
Telerik team
answered on 07 May 2013, 03:11 PM
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.
Tags
General Discussions
Asked by
Javier
Top achievements
Rank 2
Answers by
Kaloyan
Telerik team
Share this question
or