Created
January 6, 2020 14:25
-
-
Save timabell/d71ae82c6f3eaa5df26b147f9d3842eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq.Expressions; | |
using Microsoft.Extensions.Logging; | |
using Moq; | |
using Moq.Language.Flow; | |
namespace YourProject.Unit.Tests | |
{ | |
internal static class MockHelper | |
{ | |
// from https://stackoverflow.com/questions/43424095/how-to-unit-test-with-ilogger-in-asp-net-core/56728528#56728528 | |
// removed type params for logger to match our usage | |
public static ISetup<ILogger> MockLog(this Mock<ILogger> logger, LogLevel level) | |
{ | |
return logger.Setup(x => x.Log(level, It.IsAny<EventId>(), It.IsAny<object>(), It.IsAny<Exception>(), It.IsAny<Func<object, Exception, string>>())); | |
} | |
private static Expression<Action<ILogger>> Verify(LogLevel level) | |
{ | |
return x => x.Log(level, 0, It.IsAny<object>(), It.IsAny<Exception>(), It.IsAny<Func<object, Exception, string>>()); | |
} | |
public static void Verify(this Mock<ILogger> mock, LogLevel level, Times times) | |
{ | |
mock.Verify(Verify(level), times); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment