Mocking 'System.Console' behaviour

前端 未结 5 1919
南方客
南方客 2020-12-06 04:45

Is there a standard way of making a C# console application unit-testable by programming against an interface, rather than System.Console?

For example, using an ICons

5条回答
  •  無奈伤痛
    2020-12-06 05:48

    I happened to trip over this thread last night: Using reflection to override virtual method tables in C#.

    @paulo came up with the answer: LinFu by Philip Laureano.

    The usage example on Philip Laureano's Development Blog: Intercepting Console.WriteLine guides you through an example of how to intercept calls to the Console.WriteLine method, and (in this case) perform some additional operations as well... AOP for .NET... Very Noice, IMHO!

    LinFu might be just the ticket, as it has no presence within the thing being intercepted, so you can "intercept and modify" the behaviour of calls to third-party-vendor's assemblies "within your context", but without any possibility of affecting the ACTUAL behaviour of the intercepted classes outside of that context... so LinFu sounds like a darn good place to start implementing a "generic testing-framework for console apps".

    You MAY even be able to leverage one of the existing unit-testing frameworks as well. I'd be looking for an open-source framework. NUnit springs to mind. Pun intended.

    Good luck with it anyway, it's an interesting project. Keep us posted, K?

    Cheers. Keith.

提交回复
热议问题