c# - 如何使用构造函数依赖注入(inject)对 ASP.NET Core 应用程序进行单元测试

标签 c# unit-testing asp.net-core testing dependency-injection

我有一个 asp.net 核心应用程序,它使用应用程序的 startup.cs 类中定义的依赖项注入(inject):

    public void ConfigureServices(IServiceCollection services)
    {

        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration["Data:FotballConnection:DefaultConnection"]));


        // Repositories
        services.AddScoped<IUserRepository, UserRepository>();
        services.AddScoped<IUserRoleRepository, UserRoleRepository>();
        services.AddScoped<IRoleRepository, RoleRepository>();
        services.AddScoped<ILoggingRepository, LoggingRepository>();

        // Services
        services.AddScoped<IMembershipService, MembershipService>();
        services.AddScoped<IEncryptionService, EncryptionService>();

        // new repos
        services.AddScoped<IMatchService, MatchService>();
        services.AddScoped<IMatchRepository, MatchRepository>();
        services.AddScoped<IMatchBetRepository, MatchBetRepository>();
        services.AddScoped<ITeamRepository, TeamRepository>();

        services.AddScoped<IFootballAPI, FootballAPIService>();

这允许这样的事情:

[Route("api/[controller]")]
public class MatchController : AuthorizedController
{
    private readonly IMatchService _matchService;
    private readonly IMatchRepository _matchRepository;
    private readonly IMatchBetRepository _matchBetRepository;
    private readonly IUserRepository _userRepository;
    private readonly ILoggingRepository _loggingRepository;

    public MatchController(IMatchService matchService, IMatchRepository matchRepository, IMatchBetRepository matchBetRepository, ILoggingRepository loggingRepository, IUserRepository userRepository)
    {
        _matchService = matchService;
        _matchRepository = matchRepository;
        _matchBetRepository = matchBetRepository;
        _userRepository = userRepository;
        _loggingRepository = loggingRepository;
    }

这非常简洁。但是当我想进行单元测试时就成了问题。因为我的测试库没有我设置依赖注入(inject)的 startup.cs。因此,将这些接口(interface)作为参数的类将只是 null。

namespace TestLibrary
{
    public class FootballAPIService
    {
        private readonly IMatchRepository _matchRepository;
        private readonly ITeamRepository _teamRepository;

        public FootballAPIService(IMatchRepository matchRepository, ITeamRepository teamRepository)

        {
            _matchRepository = matchRepository;
            _teamRepository = teamRepository;

在上面的代码中,在测试库中,_matchRepository_teamRepository 将只是null。 :(

我可以做类似 ConfigureServices 的事情,在我的测试库项目中定义依赖注入(inject)吗?

最佳答案

虽然@Kritner 的回答是正确的,但为了代码完整性和更好的 DI 体验,我更喜欢以下内容:

[TestClass]
public class MatchRepositoryTests
{
    private readonly IMatchRepository matchRepository;

    public MatchRepositoryTests()
    {
        var services = new ServiceCollection();
        services.AddTransient<IMatchRepository, MatchRepositoryStub>();

        var serviceProvider = services.BuildServiceProvider();

        matchRepository = serviceProvider.GetService<IMatchRepository>();
    }
}

关于c# - 如何使用构造函数依赖注入(inject)对 ASP.NET Core 应用程序进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37724738/

相关文章:

c# - 使用 LINQ-to-SQL 合并两个表的内容

c# - 如何以编程方式创建 Translate Storyboard动画 UWP?

c# - 将 SQL 中的 Yes/No/Null 转换为 DataTable 中的 True/False

c# - 如何在一个解决方案中的源项目之间共享多个二进制文件?

javascript - 如何监视 JavaScript 中的递归函数

visual-studio-2013 - 如何将 ASP.NET 5 模板添加到 Visual Studio 2013 Update 5?

asp.net-core - Nopcommerce应用程序重新启动停止Visual Studio 2017

c# - 我如何在新的 coreclr 世界中模拟对象?

使用 Hilt 进行 Android Room 测试 : UserDao cannot be provided without an @Provides-annotated method

python - 生成器返回失败的 Mocks assert_called_with