Moq - Testing multiple calls to the same function

There are a couple of ways to do this that I like to use:

First is to use the SetupSequence method

That doesn't work with void methods in older versions of Moq, so if that isn't available, use the Callback method:



 public void Execute_WithRetryOnDeadlock()
        {
            InitTest();

            var processFamilyStartCalls = 0;
            _stfProcessingRepository.Setup(p => p.ProcessFamilyStart(1)).Callback(() =>
            {
                if (processFamilyStartCalls == 0)
                {
                    processFamilyStartCalls++;
                    throw new Exception("A deadlock has occurred.");
                }
            }).Verifiable();

            _sut.Execute(_stageInput, _taskInput, 1000);
            _stfProcessingRepository.Verify(p => p.ProcessFamilyStart(1), Times.Exactly(3));
        }

No comments:

Post a Comment

} else { }