Skip to content

Getting a reference to the InvocationContext #1312

Open
@woutervanranst

Description

@woutervanranst

How do I obtain the InvocationContext after the Invoke has completed? Main use case is for unit testing and examining the InvocationResult.

The current workaround below, but this is dirty.

public class Program
{
    internal InvocationContext InvocationContext { get; private set; }

    public static async Task<int> Main(string[] args)
    {
        var _ = GetCommandLineBuilder()
            .UseHost(_ => Host.CreateDefaultBuilder(),
                host =>
                {
                    InvocationContext = host.GetInvocationContext();

and the unit tests like

[Test]
public async Task Cli_NoCommand_ParseErrorResult()
{
    var args = "";

    var p = new Program();
    await p.Main(args.Split(' '));

    Assert.IsInstanceOf<ParseErrorResult>(p.InvocationContext.InvocationResult);
    Assert.AreEqual(1, p.InvocationContext.ParseResult.Errors.Count(pe => pe.Message == "Required command was not provided."));
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions