首页 > 解决方案 > 如何在c#中使用nunit为静态类中的私有静态方法编写单元测试

问题描述

我正在使用 nunit 进行单元测试,并且我想在静态类中测试私有方法。这是我的课:

public static class ServiceModuleExtentions
{
    public static void RegisterCoreServices(this IServiceCollection serviceCollection)
    {
        serviceCollection.AddScoped<ICourseService, CourseService>();
        var automapper = RegisterAutoMapperService();
        serviceCollection.AddSingleton(automapper);
    }

    private static IMapper RegisterAutoMapperService()
    {
        // Auto Mapper Configurations
        var mappingConfig = new MapperConfiguration(mc =>
        {
            mc.AddProfile(new MapperProfile());
        });

        IMapper mapper = mappingConfig.CreateMapper();

        return mapper;
    }
}

现在我的问题是如何为 RegisterAutoMapperService 方法编写单元测试?

这也是我的 MapperProfile 类:

public class MapperProfile : Profile
{
    public MapperProfile()
    {
        CreateMap<Course, CourseVM>().ReverseMap();
    }
}

标签: c#unit-testingnunitstatic-methodsstatic-classes

解决方案


推荐阅读