spectre.cli icon indicating copy to clipboard operation
spectre.cli copied to clipboard

Question: Plans to release Spectre.Cli.Testing package?

Open DarthPedro opened this issue 5 years ago • 7 comments

Hi @patriksvensson, Are you planning to release the code in Spectre.Cli.Testing as its own package? It looks very useful for testing commands end-to-end from the command line, parsed, to commands, and with verified output.

Or are we do duplicate this testing code in our projects? Or do you have other suggestions for how to test commands?

DarthPedro avatar Dec 16 '20 00:12 DarthPedro

@DarthPedro We could absolutely publish Spectre.Cli.Testing to NuGet. Would need to clean it up a bit and move some stuff used by Spectre.Cli.Tests to its own library, but I have nothing against doing this.

Give me a couple of days and I will take care of it.

patriksvensson avatar Dec 16 '20 21:12 patriksvensson

Great! It would be awesome not to have to duplicate that code in every test project.

The other option would be to give CommandContext a public constructor, so that we can create them to use in tests. That would be useful when we're just unit testing the commands and not all of the configuration and parsing. For example, I would like to be able to write a unit test like this:

    public class StudentListCommandTests
    {
        private readonly IUniversityRepository _repo = new Mock<IUniversityRepository>().Object;

        [Fact]
        public void EmptyList()
        {
            // arrange
            var command = new StudentListCommand(_repo);
            var context = new CommandContext(null, "list", null); // or new CommandContext();
            AnsiConsole.Record();

            // act
            command.Execute(context);

            // assert
            Assert.Contains("# Students: 0", AnsiConsole.ExportText());
        }
    }

However, CommandContext has an internal constructor with parameters and no default constructor, so we can't create one directly in test projects. Also, because CommandContext is a sealed class, I can't use Moq to mock the class either.

DarthPedro avatar Dec 16 '20 22:12 DarthPedro

@DarthPedro I have no objection to opening up the CommandContext. Will try to do that tonight and publish a new release.

patriksvensson avatar Dec 16 '20 22:12 patriksvensson

Awesome! Doing both of these would definitely improve command testability for consumers of Spectre.Cli.

DarthPedro avatar Dec 16 '20 22:12 DarthPedro

@DarthPedro Made the constructor public. It should be available in 0.49.0 once CI publishes it to NuGet.

patriksvensson avatar Dec 17 '20 00:12 patriksvensson

Nice work, that was fast! Thanks.

DarthPedro avatar Dec 17 '20 00:12 DarthPedro

Closing since Spectre.Cli has been moved to Spectre.Console

patriksvensson avatar Dec 30 '20 09:12 patriksvensson