Search code examples
c#unit-testingasync-awaitnunitmoq

Expected invocation on Mock once, but was 0 times


I have an interface IVehicle

public interface IVehicle
{
        Task<ApiResponse> GetVehicleInfo();
}

This is my implementation of the interface

public class Vehicle : IVehicle
{
    private string m_vehicleId;        
    private VehicleInfoEndPoint m_vEndPoint;

    public Vehicle()
    {

    }
    public Vehicle(string vehicleId, string device, HttpClient client,string Uri)
    {            
        m_vEndPoint = new VehicleInfoEndPoint(device, client, Uri);
    }
    
    public async Task<ApiResponse> GetVehicleInfo()
    {
        await m_vEndPoint.GetVehicleInfoPostAsync();
        return m_vehicleInfoEndPoint.FullResponse;
    }        
}

I want to unit test this class. For that I have written the following code.

    [Test]
    public void Vehicle_GetVehicleInformation_shouldCall_VehicleInfoEndPoint_GetVehicleInfo()
    {            
        var endPointMock = Mock.Of<IVehicleInfoEndPoint>();
        var result = new ApiResponse();
        var vehicle = new Mock<IVehicle>();

        vehicle.Setup(x => x.GetVehicleInfo()).Returns(Task.FromResult(result));

        var response = vehicle.Object.GetVehicleInfo().Result;
       
        Mock.Get(endPointMock).Verify(x => x.GetVehicleInfo(), Times.Once);
    }

My test is failing with the error that

Expected invocation on the mock once, but was 0 times x=> x.GetVehicleInfo()


Solution

  • In this case, it seems you want to test is that x.GetVehicleInfoPostAsync() is called.

    Being this the case, you have to define your unit artifacts which are:

    • Vehicle is your system under test
    • IVehicleInfoEndPoint is your mock
    • You want to assert that calling GetVehicleInfo() calls the mock endpoint

    I made this quick example that does what you want:

    class Program
    {
        static async Task Main(string[] args)
        {
            // ARRANGE
            var endPointMock = Mock.Of<IVehicleInfoEndPoint>();
            var vehicle = new Vehicle(endPointMock);
            // ACT
            var response = await vehicle.GetVehicleInfo();
            // ASSERT
            Mock.Get(endPointMock).Verify(x => x.GetVehicleInfoPostAsync(), Times.Once);
        }
    }
    public interface IVehicle
    {
        Task<ApiResponse> GetVehicleInfo();
    }
    public class Vehicle : IVehicle
    {
        private readonly IVehicleInfoEndPoint _endpoint;
        public Vehicle(IVehicleInfoEndPoint endpoint)
        {
            _endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint));
        }
        public async Task<ApiResponse> GetVehicleInfo()
        {
            await _endpoint.GetVehicleInfoPostAsync();
            return _endpoint.FullResponse;
        }        
    }
    public interface IVehicleInfoEndPoint 
    {
        Task GetVehicleInfoPostAsync();
        ApiResponse FullResponse { get; set; }
    }
    public class ApiResponse
    {
    }
    

    It helps when you divide your test into 3 parts:

    • Arrange
    • Act
    • Assert

    Check this out: What is a "Stub"?

    Also, checkout "The art of unit testing" on Amazon, great book.