Author : MD TAREQ HASSAN

Web API

IArithmeticRepository.cs

namespace XyzAPI.Repositories
{
    public interface IArithmeticRepository
    {
        public int GetRandom();
    }
}

ArithmeticRepository.cs

namespace XyzAPI.Repositories
{
    public class ArithmeticRepository : IArithmeticRepository
    {
        public int GetRandom() => new Random().Next(1, 10);
    }
}

IArithmeticService.cs

namespace XyzAPI.Services
{
    public interface IArithmeticService
    {
        public int Add(int x, int y);
    }
}

ArithmeticService.cs

using XyzAPI.Repositories;

namespace XyzAPI.Services
{
    public class ArithmeticService : IArithmeticService
    {
        private readonly IArithmeticRepository _repository;

        public ArithmeticService(IArithmeticRepository repository)
        {
            _repository = repository;
        }

        public int Add(int x, int y) => x + y;
    }
}

Program.cs

// ... ... ...

builder.Services.AddScoped<IArithmeticRepository, ArithmeticRepository>();
builder.Services.AddScoped<IArithmeticService, ArithmeticService>();

// ... ... ...

WeatherForecastControllerTestFixture.cs

using System;
using Microsoft.Extensions.Logging;
using Moq;
using XyzAPI.Controllers;

namespace XyzAPI.Test
{
    public class WeatherForecastControllerTestFixture : IDisposable
    {
        public WeatherForecastController Sut { get; private set; }

        public WeatherForecastControllerTestFixture()
        {
            var mockedLogger = new Mock<ILogger<WeatherForecastController>>();

            Sut = new WeatherForecastController(mockedLogger.Object);
        }

        public void Dispose()
        {
            //throw new NotImplementedException();
        }
    }
}

WeatherForecastControllerTest.cs

using Microsoft.AspNetCore.Mvc;
using Xunit;

namespace XyzAPI.Test
{
    public class WeatherForecastControllerTest: IClassFixture<WeatherForecastControllerTestFixture>
    {
        private readonly WeatherForecastControllerTestFixture _classFixture;

        public WeatherForecastControllerTest(WeatherForecastControllerTestFixture classFixture)
        {
            _classFixture = classFixture;
        }

        [Fact]
        public void Get_Action_Returns_OkObjectResult()
        {
            var okResult = _classFixture.Sut.Get();

            Assert.IsType<OkObjectResult>(okResult.Result);
        }
    }
}

ArithmeticServiceTestFixture.cs

using Moq;
using System;
using XyzAPI.Repositories;
using XyzAPI.Services;

namespace XyzAPI.Test
{
    public class ArithmeticServiceTestFixture : IDisposable
    {
        public ArithmeticService Sut { get; private set; }

        public ArithmeticServiceTestFixture()
        {
            var mockedRepository = new Mock<IArithmeticRepository>();

            Sut = new ArithmeticService(mockedRepository.Object);
        }

        public void Dispose()
        {
            //throw new NotImplementedException();
        }
    }
}

ArithmeticServiceTest.cs

using AutoFixture.Xunit2;
using Xunit;

namespace XyzAPI.Test
{
    public class ArithmeticServiceTest : IClassFixture<ArithmeticServiceTestFixture>
    {
        private readonly ArithmeticServiceTestFixture _classFixture;
        public ArithmeticServiceTest(ArithmeticServiceTestFixture classFixture)
        {
            _classFixture = classFixture;
        }

        [Theory]
        [InlineAutoData(2, 2, 4)]
        [InlineAutoData(2, 1, 3)]
        [InlineAutoData(5, 7, 12)]
        public void Addition_Test(int x, int y, int expectedResult)
        {
            var result = _classFixture.Sut.Add(x, y);

            Assert.Equal(expectedResult, result);
        }
    }
}

Run tests

MVC application

[pending]

Console application

[pending]

Blazor application

[pending]

CosmosDB

Unit testing using CosmosDB emulator