I use a Util-Method that is an Extension-method delaying It.*-calls until the Setup (using the result of an It.*-call stored in a variable doesn't seem to work).
However, I noticed that when I call the Func<string> message
with message()
, the Setup will not work properly. When using message.Invoke()
, it works just as I expect.
As far as I understand, message()
should be syntactic sugar for message.Invoke()
, but then why do they behave differently?
I do believe that has something to do with the Moq-Framework, that's why I tagged this moq. Something like Func<string> f = ()=>"test"; f();
returns a string. Moq does lots of magic with It.*, Mock<> and so on, maybe I interfered with it somehow.
Following example:
public interface ILog
{
void Debug(string message, Exception exception = null);
}
public class LogUtils
{
public static void Debug(this Mock<ILog> mock, string message, Exception exception = null)
{
mock.Debug(() => message, () => exception);
}
public static void Debug(this Mock<ILog> mock, Func<string> message, Func<Exception> exception)
{
// NOT working line
mock.Setup(logger => logger.Debug(message(), exception()));
// Working line
mock.Setup(logger => logger.Debug(message.Invoke(), exception.Invoke()));
}
}
// Test Method
public void TestMethod()
{
Mock<ILog> logger = new Mock<ILog>(MockBehavior.Strict);
// Setup logger to accept any input by using It.IsAny<>-MethodGroup
logger.Debug(It.IsAny<string>, It.IsAny<Exception>);
// Fails with no corresponding setup
logger.Object.Debug("Test");
}