.net core 3.1 单元测试

直接上代码

  • 借助Mock来解决依赖注入的问题 mock+实际依赖(mapper)
    • 直接Mock new 对象的方式
    • 使用AutoFixtrue自动构造所有依赖
  • 后面再写一篇集成测试
using AutoFixture;
using AutoFixture.AutoMoq;
using AutoFixture.Xunit2;
using GenFu;
using Microsoft.Extensions.Logging;
using Moq;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using UnitTests.Common;
using Walle.Domain.Models.Sys;
using Walle.Domain.Service.Services;
using Walle.Integration.DsfAuthority.Interface;
using Xunit;

namespace UnitTests
{
    public class SysDeptServiceTests
    {
        /// 
        /// 案例1: 使用Mock方式
        /// 
        /// 
        [Theory]
        [InlineData(1)]
        public void GetDeptConfigAsync_Id_ShouldBy_Equal(int id)
        {
            // arrange
            var repository = CreateDbContext();
            var oaDeptService = new Mock();
            var logger = new Mock>();
            var service = new SysDeptService(repository.Object, oaDeptService.Object, logger.Object, AutoMap.Get());

            //act
            var result = service.GetDeptConfigAsync(id);
            //assert
            Assert.Equal(id, result.Result.Id);
        }

        /// 
        /// 案例二: 使用AutoFixtrue+Mock方式
        /// 使用xunit自带给参数赋值方式 InlineData可以重复多次
        /// 
        /// 
        [Theory]
        [InlineData(1)]
        public void GetDeptConfigAsync_AutoDI_ShouldBy_True(int id)
        {
            // arrange
            var deptConfig = GetFakeData().AsQueryable().First();

            var fix = new Fixture();
            //自动构造所有依赖
            fix.Customize(new AutoMoqCustomization());
            //Mock返回结果
            var free = fix.Freeze>();
            free.Setup(p => p.FindByDeptIdAsync(It.IsAny())).Returns(Task.FromResult(deptConfig));
            //注入实际依赖(真是依赖代替mock)
            fix.Customizations.Add(new MockAutoMap());

            var psn = fix.Create();

            //act
            var result = psn.GetDeptConfigAsync(id);
            //assert
            Assert.Equal(id, result.Result.Id);
        }
        /// 
        /// 使用AutoFixture自动给参数赋值  支持对象 枚举…… 
        /// 高级篇: https://www.cnblogs.com/tylerzhou/p/11392248.html
        /// 
        /// 
        [Theory]
        [AutoData]
        public void GetDeptConfigAsync_AutoId_ShouldBy_True(int id)
        {

            // arrange
            var repository = CreateDbContext();
            var oaDeptService = new Mock();
            var logger = new Mock>();
            var service = new SysDeptService(repository.Object, oaDeptService.Object, logger.Object, AutoMap.Get());

            //act
            var result = service.GetDeptConfigAsync(id);
            //assert
            Assert.True(result.Result.Id > 0);
        }
        #region private method
        /// 
        /// 获取ISysDeptConfigRepository的Mock
        /// 
        /// 
        private Mock CreateDbContext()
        {
            var context = new Mock();

            var deptConfig = GetFakeData().AsQueryable().First();
            context.Setup(p => p.FindByDeptIdAsync(It.IsAny())).Returns(Task.FromResult(deptConfig));

            return context;
        }
        /// 
        /// 通过Genfu自动生成测试数据
        /// 
        /// 
        private IEnumerable GetFakeData()
        {
            var i = 1;
            var sysDeptConfig = A.ListOf(26);
            sysDeptConfig.ForEach(x => x.Id = i++);
            return sysDeptConfig.Select(_ => _);
        }
        #endregion
    }
}


using AutoFixture.Kernel;
using AutoMapper;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using Walle.Domain.Service.MapperProfiles;
using Walle.Domain.Service.Services;

namespace UnitTests.Common
{
    /// 
    /// 方式一、替换掉Mock使用
    /// 
    public static class AutoMap
    {
        public static IMapper Get()
        {
            var services = new ServiceCollection();
            services.AddAutoMapper(typeof(DomainMapperProfiles));
            var scope = services.BuildServiceProvider().CreateScope();
            IMapper _mapper = scope.ServiceProvider.GetService();
            return _mapper;
        }
    }
    /// 
    /// 方式二、替换AutofixTrue的自动生成的接口
    /// 
    public class MockAutoMap : ISpecimenBuilder
    {
        public object Create(object request, ISpecimenContext context)
        {
            if (!(request is ParameterInfo pi))
                return new NoSpecimen();
            if (pi.Member.DeclaringType != typeof(SysDeptService) ||
                pi.ParameterType != typeof(IMapper) ||
                pi.Name != "mapper")
                return new NoSpecimen();

            var services = new ServiceCollection();
            services.AddAutoMapper(typeof(DomainMapperProfiles));
            var scope = services.BuildServiceProvider().CreateScope();
            return  scope.ServiceProvider.GetService();
        }
    }
}

你可能感兴趣的:(.net core 3.1 单元测试)