简体   繁体   English

如何在 c# 中模拟用于单元测试的记录器?

[英]How to mock a logger for unit testing in c#?

The method that i need to unit test logs the output in the following way and doesnt return anything.我需要进行单元测试的方法以下列方式记录 output 并且不返回任何内容。 How do i write a unit test method for the following case?我如何为以下情况编写单元测试方法? Please help i'm new to this concept and havent written any test methods before请帮助我是这个概念的新手,之前没有写过任何测试方法

private void revoke (params) {
    if (userexist (revokedb, userid))``
{
        deleteusers (revokedb, userid);
        if (userexist (revokedb, userid))
       {
           logger.info ("Not able to delete user");
       }
       else
       {
          logger.info ("User was succesfully deleted");
       }
    else
  {
       logger.info ("unable of find the user");
  }
}

Check out moq https://github.com/Moq/moq4查看起订量moq ://github.com/Moq/moq4

your logger needs to implement an interface that you can then mock.您的记录器需要实现一个接口,然后您可以模拟。

Eg例如

MyLogger:ILogger

ILogger would define the method info(string) ILogger将定义方法info(string)

in your test, you'd set up your mock first在你的测试中,你首先设置你的模拟

var mock = new Mock<ILoveThisLibrary>();

and then you can inject this mock into your class (where logger ) is used.然后您可以将此模拟注入您的 class (其中使用logger )。 Either through the constructir or any other method that replaces the actual logger with this mock object.通过构造函数或任何其他方法用这个模拟 object 替换实际记录器。 This is called dependency injection.这称为依赖注入。

Now all calls to logger will get directed to your mock object and you can do all sorts of mocking.现在所有对logger的调用都将定向到您的模拟 object,您可以执行各种 mocking。 Eg expectations, ie checking that info was called with the string that you expect.例如期望,即检查使用您期望的字符串调用的info

I think you can inject the logger in the class constructor like below.我认为您可以在 class 构造函数中注入记录器,如下所示。

public class BusService : IBusService
    {
        private readonly ILogger _logger;
        public BusService(ILogger<BusService> logger)
        {
            _logger = logger;
        }
        private void revoke (params) {
    if (userexist (revokedb, userid))``
    {
        deleteusers (revokedb, userid);
        if (userexist (revokedb, userid))
       {
           _logger.info ("Not able to delete user");
       }
       else
       {
          _logger.info ("User was successfully deleted");
       }
    else
    {
       _logger.info ("unable of find the user");
    }
  }
}

Once it is injected in class constructor like above, you can use Moq class to mock your logger like below test method.一旦像上面那样将它注入到 class 构造函数中,您就可以使用 Moq class 来模拟您的记录器,如下面的测试方法。

[Fact]
public void BusService_revoke_deleteUser()
        {
            // Arrange
            var logger = Mock.Of<ILogger<BusService>>();
            
            var busService = new BusService(logger);
            
            // Act
            var result = busService.revoke(params);

            // Assert
             // Assert statement will go here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM