Facing error when trying to mock a Method in Concrete Class
Below is my code and the test I am trying to write for CoreException class
Inside CoreException I am calling LogMgr which is a concrete class
public class CoreException
{
public CoreException()
{
}
public void HandleException(string inErr)
{
LogMgr.Instance.Trace("", "");
}
}
public class LogMgr
{
private static LogMgr mInstance = null;
public static LogMgr Instance
{
get
{
if ((null == mInstance) )
{
mInstance = new LogMgr();
}
return mInstance;
}
}
public void Trace(string logFormat, string logParams)
{
Console.WriteLine("Log Saved");
}
}
Below are my Unit Tests…..
namespace TestProject1
{
public static class LogMgrMock
{
public static Mock<LogMgr> GetMock()
{
var mock = new Mock<LogMgr>();
mock.Setup(m => m.Trace(It.IsAny<string>(), It.IsAny<string>())).Verifiable();
return mock;
}
}
public class CoreExceptionTests
{
[Fact]
public void TestException()
{
var coreExec = new CoreException();
var mockLogMgr = LogMgrMock.GetMock();
baservException.HandleException("Test");
mockLogMgr.Verify(m => m.Trace(It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
}
}
Below is the exception Iam facing
System.NotSupportedException : Unsupported expression: m => m.Trace(It.IsAny<string>(), It.IsAny<string>()..... Non-overridable members (here: LogMgr.Trace) may not be used in setup / verification expressions