TypeMock - 伪造 Console.Error.WriteLine()

TypeMock - faking Console.Error.WriteLine()

我正在尝试使用 TypeMock 伪造控制台输出日志记录:

[Test]
public void Log_Error_LogsToStdErr()
{
    var logger = SetupFakeLogger();

    logger.Log( "text to log", true );

    Isolate.Verify.WasCalledWithAnyArguments( () => logger.LogToFile( "" ) );
    Isolate.Verify.WasCalledWithAnyArguments( () => Console.Error.WriteLine( "" ) );
}

private static WrapperLogger SetupFakeLogger()
{
    Isolate.Fake.StaticMethods( typeof(Console), Members.ReturnRecursiveFakes );

    Isolate.WhenCalled( () => Console.Error.WriteLine( "" ) ).IgnoreCall();    

    return Isolate.Fake.Instance<WrapperLogger>( Members.CallOriginal, ConstructorWillBe.Called, "dir", "file.log" );
}

但是我在我的第二个断言中遇到了以下异常(调用 Isolate.Verify for Console.Error.WriteLine):

TypeMock Verification: Method System.Environment.NewLine was expected but was not called

这是某种错误,还是我做错了?

免责声明我在 Typemock 工作。

我们不支持伪造Console,请查看我们的supported fakeable mscorlib types

您可以使用 TextWriter 而不是控制台,它会起作用。

参见示例:

 [TestMethod]
        public void Log_Error_LogsToStdErr()
        {
            var logger = SetupFakeLogger_Works();

            logger.Log("text to log", true);

            Isolate.Verify.WasCalledWithAnyArguments(() => logger.LogToFile(""));
            //Isolate.Verify.WasCalledWithAnyArguments(() => Console.Error.WriteLine(""));
        }

        private static WrapperLogger SetupFakeLogger_Works()
        {
            var textWriter = Isolate.Fake.Instance<TextWriter>();
            Console.SetOut(textWriter);

            return Isolate.Fake.Instance<WrapperLogger>(Members.CallOriginal, ConstructorWillBe.Called, "dir", "file.log");
        }