What is the correct way to handle user input to IAnsiConsole when implementing unit tests? #1524
-
I have some implementations that leverage the How can I mock user input for these within the context of unit tests? Since these are extension methods, there isn't an easy way to mock the behavior here and I didn't see an obvious way of using |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Take a look at TestConsoleInput, specifically "PushText" and "PushTextWithEnter". I have used the following code in my unit test to mock user input:
|
Beta Was this translation helpful? Give feedback.
-
@wbaldoumas There are quite a few interactive unit tests located here, which you might find provide the pattern you are looking for: https://github.com/spectreconsole/spectre.console/tree/main/src/Tests/Spectre.Console.Tests/Unit/Prompts Eg: [Fact]
public void Should_Select_The_First_Leaf_Item()
{
// Given
var console = new TestConsole();
console.Profile.Capabilities.Interactive = true;
console.Input.PushKey(ConsoleKey.Enter);
// When
var prompt = new SelectionPrompt<string>()
.Title("Select one")
.Mode(SelectionMode.Leaf)
.AddChoiceGroup("Group one", "A", "B")
.AddChoiceGroup("Group two", "C", "D");
var selection = prompt.Show(console);
// Then
selection.ShouldBe("A");
} |
Beta Was this translation helpful? Give feedback.
@wbaldoumas There are quite a few interactive unit tests located here, which you might find provide the pattern you are looking for: https://github.com/spectreconsole/spectre.console/tree/main/src/Tests/Spectre.Console.Tests/Unit/Prompts
Eg: