Home > Software engineering >  Show "No invocations performed" error when write "Verify" test method
Show "No invocations performed" error when write "Verify" test method

Time:09-19

We have a service that is being used inside another service:

namespace ConsoleCalculator.Services
{
    public interface ICalculatorService
    {
        int GetLineCount(string inputLineCount);
    }
}

namespace ConsoleCalculator.Services
{
    public class CalculatorService : ICalculatorService
    {
        private readonly ICalculatorValidationService _calculatorValidationService;

        public CalculatorService(ICalculatorValidationService calculatorValidationService)
        {
            _calculatorValidationService = calculatorValidationService;
        }

        public int GetLineCount(string inputLineCount)
        {
            _calculatorValidationService.ValidateLineCount(inputLineCount);
            return Convert.ToInt32(inputLineCount);
        }
    }
}

Now we need to write a test for the desired service:

namespace ConsoleCalculator.Test.Services
{
    public class CalculatorServiceTest
    {
        private Mock<ICalculatorService> _calculatorService = new Mock<ICalculatorService>();
        private Mock<ICalculatorValidationService> _valculatorValidationService = new Mock<ICalculatorValidationService>();
        public CalculatorServiceTest()
        {
            _valculatorValidationService
              .Setup(q => q.ValidateLineCount(It.IsRegex("[^0-9]")))
              .Throws(new Exception());

            _calculatorService
                .Setup(q => q.GetLineCount(It.IsRegex("[^0-9]")))
                .Throws(new Exception());
        }


        [Fact]
        public void When_Input_Not_A_Number_Should_Return_Exception()
        {
            Assert.Throws<Exception>(() => _calculatorService.Object.GetLineCount("123a"));
        }

        [Fact]
        public void When_Call_Method_Should_Call_Exactaly_One_ValidateLineCount_Method()
        {
            _calculatorService.Object.GetLineCount(It.IsAny<string>());
            _valculatorValidationService.Verify(x => x.ValidateLineCount(It.IsAny<string>()), Times.Once);
        }
    }
}

The purpose of writing the second test was to make sure that my test service is called once. But when running the test, I see the following error:

Moq.MockException : Expected invocation on the mock once, but was 0 times: x => x.ValidateLineCount(It.IsAny())

CodePudding user response:

Based on the moq you have given, the validation service method will not be invoked as an exception is thrown on calling GetLineCount and hence the test fails.

Instead of setting this moq

_calculatorService
                .Setup(q => q.GetLineCount(It.IsRegex("[^0-9]")))
                .Throws(new Exception());

Try like this

[Fact]
public void When_Call_Method_Should_Call_Exactaly_One_ValidateLineCount_Method()
{
         
var calSrvc = new CalculatorService(_valculatorValidationService.Object);
 
calSrvc.Object.GetLineCount(It.IsAny<string>());
   
_valculatorValidationService.Verify(x => x.ValidateLineCount(It.IsAny<string>()), Times.Once);
}
  • Related