How to unit test with ILogger in ASP.NET Core

后端 未结 14 1229
南旧
南旧 2020-12-04 11:47

This is my controller:

public class BlogController : Controller
{
    private IDAO _blogDAO;
    private readonly ILogger _         


        
14条回答
  •  死守一世寂寞
    2020-12-04 12:14

    Already mentioned you can mock it as any other interface.

    var logger = new Mock>();
    

    So far so good.

    Nice thing is that you can use Moq to verify that certain calls have been performed. For instance here I check that the log has been called with a particular Exception.

    logger.Verify(m => m.Log(It.Is(l => l == LogLevel.Information), 0,
                It.IsAny(), It.IsAny(), It.IsAny>()));
    
    
    

    When using Verify the point is to do it against the real Log method from the ILooger interface and not the extension methods.

    提交回复
    热议问题