-
Notifications
You must be signed in to change notification settings - Fork 53
Mocks
Here are listed various Spy/Mocks/Stubs that are provided by NFluent.
CaptureConsole is here to help you write checks related to Console usage. While I think most its uses will be limited to code Katas, you can use it to TDD any console application/library.
CaptureConsole is used for block of checks. It implements RAII pattern through IDisposable: when build it redirect console streams and restore previous behaviour when disposed. Remember that Console.Read and Console.ReadLine methods are blocking until input is provided. It may be wise to write console related tests in an async fashion.
Warning failing to Dispose CaptureConsole will break your tests
Example:
using(var capture = new CaptureConsole())
{
...
DoSomeStuf();
// check that DoSomeStuff printed the expect result
Check.That(capture.ReadLine()).IsEqualTo(_expectedResult_);
}
The class can be used recursively, but only the latest instance has actual access to the console.
Gets the text that has been written to the console as a string, including newline markers. Example:
using(var capture = new CaptureConsole())
{
...
Console.WriteLine("ABC");
Check.That(capture.Output).IsEqualTo("ABC"); // Fail
Check.That(capture.Output).IsEqualTo("ABC"+Environment.NewLine); // Success
}
It could be handy to use the AsLines
extension.
Example:
using (var capture = new CaptureConsole())
{
...
Console.WriteLine("ABC");
Console.WriteLine("DEF");
Check.That(capture.Output)
.AsLines()
.ContainsExactly("ABC", "DEF", "");
...
}
And yes, even if you call only twice Console.WriteLine
, there are 3 lines and the last one is empty.
If you really want to avoid this, you can trim the last new line.
Example:
using (var capture = new CaptureConsole())
{
...
Console.WriteLine("ABC");
Console.WriteLine("DEF");
Check.That(capture.Output.TrimEnd(Environment.NewLine.ToCharArray()))
.AsLines()
.ContainsExactly("ABC", "DEF");
...
}
Simulate text input to the console. Example:
using(var capture = new CaptureConsole())
{
...
capture.Input("hello");
var text = Console.Read();
// this test succeeds
Check.That(text).IsEqualTo('h');
}
Remember: you need to provide the text before the tested code calls Console.ReadLine (or Read), otherwise, your test may deadlock.
Simulate line input to the console. Example:
using(var capture = new CaptureConsole())
{
...
capture.InputLine("hello");
var text = Console.ReadLine();
// this test succeeds
Check.That(text).IsEqualTo("hello");
}
Read a line from the console output. It allows to get the console content on a one by line basis. Example:
using(var capture = new CaptureConsole())
{
...
Console.WriteLine("hello");
var text = capture.ReadLine();
// this test succeeds
Check.That(text).IsEqualTo("hello");
}
Remember: you need to provide the text before the tested code calls Console.ReadLine (or Read), otherwise, your test may deadlock.
- Welcome
- How to start
- Customising error messages
-
Check.That
- All (Equality, Type)
- Reference types
- Members based
- IEnumerable (Properties, Content, Elements)
- String (Properties, Content, RegExp )
- Numeric Type(Properties, Comparisons, Floating)
- Dictionary
- Char (Properties, Value)
- IComparable
- DateTime
- DateTimeOffset
- Misc: Boolean, TimeSpan, Stream, Enum, EventWaitHandle
- Check.ThatCode
- Check.ThatDynamic
- Extensibility
- Auxiliary types (TimeUnit)
- Mocks