Files
spectreconsole-examples/examples/Cli/Testing/CommandAppTests.cs
Frank Ray b030977c0a Add CommandApp unit testing example (#2)
* Added console interactivity test
* Added `Should_Render_Panel` test
2025-04-08 16:59:47 +02:00

47 lines
1.1 KiB
C#

using Spectre.Console;
using Spectre.Console.Cli;
using Spectre.Console.Testing;
namespace Testing;
[TestClass]
public class CommandAppTests
{
/// <summary>
/// A Spectre.Console Command
/// </summary>
public class HelloWorldCommand : Command
{
private readonly IAnsiConsole _console;
public HelloWorldCommand(IAnsiConsole console)
{
// nb. AnsiConsole should not be called directly by the command
// since this doesn't play well with testing. Instead,
// the command should inject a IAnsiConsole and use that.
_console = console;
}
public override int Execute(CommandContext context)
{
_console.WriteLine("Hello world.");
return 0;
}
}
[TestMethod]
public void Should_Output_Hello_World()
{
// Given
var app = new CommandAppTester();
app.SetDefaultCommand<HelloWorldCommand>();
// When
var result = app.Run();
// Then
Assert.AreEqual(result.ExitCode, 0);
Assert.AreEqual(result.Output, "Hello world.");
}
}