Question: Plans to release Spectre.Cli.Testing package?
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 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.
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 I have no objection to opening up the CommandContext. Will try to do that tonight and publish a new release.
Awesome! Doing both of these would definitely improve command testability for consumers of Spectre.Cli.
@DarthPedro Made the constructor public. It should be available in 0.49.0 once CI publishes it to NuGet.
Nice work, that was fast! Thanks.
Closing since Spectre.Cli has been moved to Spectre.Console