Skip to content

Unit test Spectre.Console.Cli app console output always blank #1232

Closed Answered by karlsloman
karlsloman asked this question in Q&A
Discussion options

You must be logged in to vote

I might have answered my own question by using a combination of Dependency Injection and CommandAppTester.

So in Program.cs Main method

var services = new ServiceCollection()
    .AddSingleton(AnsiConsole.Console); // This seems to be the fix I needed (along with injection in the CommandApp itself)

var app = new CommandApp(new TypeRegistrar(services));

app.Configure(config =>
{
    config.AddCommand<GenerateCardNumberCommand>("cardnumber");
};

return app.Run(args);

Injected the IAnsiConsole into the GenerateCardNumberCommand

private readonly IAnsiConsole _ansiConsole;

public GenerateCardNumberCommand(IAnsiConsole ansiConsole)
{
    _ansiConsole = ansiConsole;
}

public override int E…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by karlsloman
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant