Skip to content

Instantly share code, notes, and snippets.

@timabell
Created January 6, 2020 14:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timabell/d71ae82c6f3eaa5df26b147f9d3842eb to your computer and use it in GitHub Desktop.
Save timabell/d71ae82c6f3eaa5df26b147f9d3842eb to your computer and use it in GitHub Desktop.
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