小编HO *_*Pin的帖子

如何在单元测试中调用依赖注入类方法?

我是单元测试和 DI 的新手,无法找到一种简单的方法来调用使用依赖项注入设计的类中的方法。

这是我的班级

public class AgentProvisioningServiceHelpher : IAgentProvisioningServiceHelpher
{
    private readonly IExcelParser _excelParser;
    private readonly SupervisorDbContext _SupervisorDbContext;
    private readonly SchedulerNoTrackingDbContext _SchedulerDbContext;

    // constructor
    public AgentProvisioningServiceHelpher(IExcelParser excelParser, SupervisorDbContext supervisorDbContext, SchedulerNoTrackingDbContext SchedulerDbContext)
    {
        _excelParser = excelParser;
        _SupervisorDbContext = supervisorDbContext;
        _SchedulerDbContext = SchedulerDbContext;
    }

    // Function that I want to call in unit test
    public int SimpleMethodToTest(int InputId) 
    {
        return InputId + 1;
    } 
}
Run Code Online (Sandbox Code Playgroud)

这是我的界面代码

    public interface IAgentProvisioningServiceHelpher
    {
        int SimpleMethodToTest(int InputId);
    }
Run Code Online (Sandbox Code Playgroud)

这是我的单元测试代码,我使用的是 Xunit

public class UnitTest1
{
    private IAgentProvisioningServiceHelpher …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing dependency-injection xunit.net

7
推荐指数
1
解决办法
4342
查看次数