How to mock a method that returns a list of interfaces

This is my repository method:

    public async Task<IList<IVehicleMake>> GetAllMakes()
    {
        var makes = Context.VehicleMakes.AsQueryable();

        return new List<IVehicleMake>(AutoMapper.Mapper.Map<List<VehicleMake>>(await makes.ToListAsync()));
    }

I know how to mock the method that returns a concrete type, but as you can see, this method returns a list of Interfaces "IVehicleMake".

Is it possible to mock this kind of method and how?

1 answer

  • answered 2018-07-11 06:35 asidis

    You can simply use a mocking framework such as Moq. It is not so different than mocking a simple interface.

    Here is a sample code using MS Test and Moq

    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Moq;
    
    namespace UnitTestProject1
    {
        public class Vehicle{}
        public interface IVehicleMake
        {
            Vehicle Make();
        }
        public interface IVehicleMakeRepository
        {
            Task<IList<IVehicleMake>> GetAllMakes();
        }
    
        public class MyService
        {
            private readonly IVehicleMakeRepository vehicleMakeRepository;
    
            public MyService(IVehicleMakeRepository vehicleMakeRepository)
            {
                this.vehicleMakeRepository = vehicleMakeRepository;
            }
    
            public async Task DoSomething()
            {
                var makes = await vehicleMakeRepository.GetAllMakes();
                foreach (var make in makes)
                {
                    var vehicule = make.Make();
                }
            }
        }
    
        public class MyServiceTest
        {
            [TestClass]
            public class DoSomething
            {
                [TestMethod]
                public async Task Should_CallAllMakersOnce()
                {
                    //Arrange
                    var mockedMakers = new[] { new Mock<IVehicleMake>(), new Mock<IVehicleMake>(), };
                    foreach (var mockedMaker in mockedMakers)
                    {
                        mockedMaker.Setup(m => m.Make()).Returns(new Vehicle()).Verifiable();
                    }
                    var mockedRepository = new Mock<IVehicleMakeRepository>();
                    mockedRepository
                        .Setup(m => m.GetAllMakes())
                        .ReturnsAsync(mockedMakers.Select(mm => mm.Object).ToList())
                        .Verifiable();
                    var service = new MyService(mockedRepository.Object);
                    //Act
                    await service.DoSomething();
                    //Assert
                    mockedRepository.Verify(m => m.GetAllMakes(), Times.Once);
                    foreach (var mockedMaker in mockedMakers)
                    {
                        mockedMaker.Verify(m => m.Make(), Times.Once);
                    }
                }
            }
        }
    }