Skip to content

Changes needed so ICommandHandler classes are testable #1634

Open
@dougclutter

Description

@dougclutter

I'm using Beta 2, so please excuse (ignore) me if this has already been addressed.

The ICommandHandler.InvokeAsync method takes an InvocationContext parameter. Unfortunately, I couldn't find a way to instantiate an InvocationContext in my unit tests, so I couldn't suss out how to test it...

Perhaps, this parameter could be changed to an interface?

Activity

jonsequitur

jonsequitur commented on Feb 13, 2022

@jonsequitur
Contributor

You can do this:

var command = new RootCommand();
command.SetHandler((InvocationContext ctx) => ctx.Console.Out.Write("hello!"));
var testConsole = new TestConsole();
var parseResult = command.Parse();
var context = new InvocationContext(parseResult, testConsole);
await command.Handler.InvokeAsync(context);
// check output using testConsole.Out.ToString()

In practice though, this is simpler:

var command = new RootCommand();
command.SetHandler((InvocationContext ctx) => ctx.Console.Out.Write("hello!"));
var testConsole = new TestConsole();
await command.InvokeAsync("", testConsole);
// check output using testConsole.Out.ToString()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @jonsequitur@dougclutter

        Issue actions

          Changes needed so ICommandHandler classes are testable · Issue #1634 · dotnet/command-line-api