【问题标题】:How to mock the dependency injection object in unit test如何在单元测试中模拟依赖注入对象
【发布时间】:2021-07-06 18:07:58
【问题描述】:

我的项目需要单元测试。我在我的控制器中使用构造函数依赖注入。当我在我的单元测试项目中模拟注入的依赖对象并在测试方法中调用它时。在所有情况下都返回 null。

控制器类:

public class Owner:Controller
{
    private readonly IComRepository repository;
    private readonly DbContext context;
    
    public Owner(IComRepository repository, DbContext context)
    {
      this.repository=repository;
      this.context=context;
    }

    [HttpGet("GetAllTypes")]
    public async Task<IActionResult> GetAllTypes()
    {
      var ownerTypes=repository.GetTypes();
        return Ok(ownerTypes);
    }
}

我的存储库类

public Interface IComRepository
{
    IList<Type> GetTypes();
}
        
public Class ComRepository : IComRepository
{
    private readonly DbContext context;
    public ComRepository(DbContext context)
    {
        this.context=context;
    }
    public IList<Type> GetTypes()
    {
        var allTypes= context.Types.ToList();
        return allTypes;
    }
}

现在我需要在我的控制器类中测试 GetAllTypes 方法。下面提到了我的测试类:

using moq;
[TestClass]
public Class OwnerTest
{
    public OwnerTest()
    {
        var mockIcomrepo = new Mock<IComRepository>();
        var mockDbcontext = new Mock<Dbcontext>();
        OwnerController owner = new OwnerController(mockDbContext.object,mockIcomrepo.object);
    
    }
    [TestMethod]
    public void GetTypes()
    {
        var allTypes= owner.GetAllTypes(); //It's not trigger to my controller
        Assert.AreEqual(5,allTypes.count());
    }
}

我该怎么做?任何人都知道这个问题的答案。

【问题讨论】:

  • 模拟仓库没有设置做任何事情,所以默认情况下它会返回 null。
  • 参考Moq Quickstart以更好地了解如何使用起订量
  • 如果答案解决了你的问题,你能把它标记为答案

标签: c# unit-testing mocking moq


【解决方案1】:

正如@Nkosi 提到的,您必须使用最小起订量设置。在构造函数之外定义你的模拟并在测试类的构造函数中初始化它们。

using moq;
[TestClass]
public Class OwnerTest
{
    private readonly IComRepository _mockRepository;
    private readonly OwnerControler _ownerController;
    
    //your mock data
    private readonly IList<Type> mockData; 

    public OwnerTest()
    {
        _mockRepository= new Mock<IComRepository>();

        _ownerController = new OwnerController(mockDbContext.object,mockIcomrepo.object);
        mockData=new IList<Type>{"Data1","Data2","Data3","Data4","Data5"};

    }
    
    //choose better names for testing a method 
    //Naming convention like this MethodName_StateUnderTest_ExpectedResult;
    
    [TestMethod]
    public void GetAllTypes() 
    {
        _mockRepository.Setup(p=>p.GetAllTypes()).Returns(mockData);

        var result= _ownerController.GetAllTypes();
        var okResult=Assert.IsType<OkObjectResult>(result)
        var returnTypes=Assert.IsAssignableFrom<IList<Type>>(okResult.Value);
        Assert.AreEqual(5,returnTypes.count());
    }
}

另外,为什么要将 dbcontext 注入控制器?您的存储库应该依赖 dbcontext 而不是控制器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    • 2021-06-19
    • 1970-01-01
    • 2019-11-27
    相关资源
    最近更新 更多