Added a TypeRegistrar to CommandAppTester

exposed as a public property.
For convenience, and to keep the similarity to the real CommandApp
it is also available in the ctor of CommandAppTester.
This commit is contained in:
Nils Andresen
2021-06-21 15:52:12 +02:00
committed by Patrik Svensson
parent b92827ce3d
commit 6b5b31957c
4 changed files with 91 additions and 3 deletions

View File

@ -1,3 +1,5 @@
using System;
using System.Collections.Generic;
using Shouldly;
using Spectre.Console.Testing;
using Spectre.Console.Tests.Data;
@ -30,7 +32,7 @@ namespace Spectre.Console.Tests.Unit.Cli
Name = "Hello " + name;
}
}
[Fact]
public void Should_Inject_Parameters()
{
@ -62,6 +64,36 @@ namespace Spectre.Console.Tests.Unit.Cli
injected.Age.ShouldBe(35);
});
}
[Fact]
public void Should_Inject_Dependency_Using_A_Given_Registrar()
{
// Given
var dependency = new FakeDependency();
var registrar = new FakeTypeRegistrar { TypeResolverFactory = FakeTypeResolver.Factory };
registrar.RegisterInstance(typeof(FakeDependency), dependency);
var app = new CommandAppTester(registrar);
app.SetDefaultCommand<GenericCommand<InjectSettings>>();
app.Configure(config => config.PropagateExceptions());
// When
var result = app.Run(new[]
{
"--name", "foo",
"--age", "35",
});
// Then
result.ExitCode.ShouldBe(0);
result.Settings.ShouldBeOfType<InjectSettings>().And(injected =>
{
injected.ShouldNotBeNull();
injected.Fake.ShouldBeSameAs(dependency);
injected.Name.ShouldBe("Hello foo");
injected.Age.ShouldBe(35);
});
}
}
}
}