Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.CommandLine CLI parsing library won't call a method on a mocked object #1182

Closed
bradykelly opened this issue Jul 1, 2021 · 2 comments

Comments

@bradykelly
Copy link

System.CommandLine is a package from MS for parsing command-line arguments. It fails to do this properly when asked to invoke a method on a mocked object.

With the following code in ApplicationService:

public async Task<int> RunAsync(string[] cliArgs)
{
    var rootCommand = BuildCommandTree();

    rootCommand.Handler = CommandHandler.Create(delegate (string table, string? model, string? schema, bool overwrite, IConsole console)
    {
        _builderService.BuildForSingleTable(table, model, schema, overwrite);
    });
    //var returnCode = await rootCommand.InvokeAsync(cliArgs);
    var returnCode = await rootCommand.InvokeAsync(cliArgs);
    return returnCode;
}

and this mocking code:

    _builderMock.Setup(x => x.BuildForSingleTable(
        It.Is<string>(s => s.Equals(tableName)), 
        It.Is<string>(s => s.Equals(modelName)), 
        It.Is<string>(s => s.Equals(schemaName)), 
        It.Is<bool>(x => x))).Verifiable();
        
var tempSut = new ApplicationService(_loggerMock.Object, _configMock.Object, _builderMock.Object);
await tempSut.RunAsync(args);        

where _builderService and _builderMock are both IBuilderService, but RunAsync only works when _builderService is a real BuilderService and not a mock.

When _builderService is a mock BuildForSingleTable is never called and InvokeAsync returns 1, indicating an error I presume, but when _builderService is a concrete BuilderService, InvokeAsync returns 0 and my method is called.

I thought I'd open an issue because here Moq is failing in what it's supposed to do: appear to a method as a normal object. I have also reported this on the System.CommandLine repo. I am using Moq

@stakx
Copy link
Contributor

stakx commented Jul 1, 2021

It's a bit tedious having to piece together a repro from prose. Please post a minimal but complete code repro, otherwise there's nothing we can do to help.

@bradykelly
Copy link
Author

I have since found out that using a stub with a string[] array passed as args has the same effect. I may have something wrong with my args setup so I will close this and wait and see before I open another one. Sorry about that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants