I took a legacy .NET framework application, that consisted of class libraries and a unit test project only, and converted them all to .NET core 3.1.
I can't seem to execute or bootstrap the unit tests with the .NET core method of using the IHostBuilder
's CreateHostBuilder
.
When I debug any of my unit tests, it stops executing and I get this error:
The following constructor parameters did not have matching fixture data: IOptions`1 appConfig, IServiceX serviceX, ITemplateEngine templateEngine Exception doesn't have a stacktrace
Sample unit test:
public class TemplateGenerationTests : BaseTest
{
private readonly AppConfiguration appConfig;
private readonly IServiceX serviceX;
private readonly ITemplateEngine templateEngine;
public TemplateGenerationTests(IOptions<AppConfiguration> appConfig, IServiceX serviceX, ITemplateEngine templateEngine)
{
this.appConfig = appConfig.Value;
this.serviceX = serviceX;
this.templateEngine = templateEngine;
}
[Fact]
public void SomeTestIhave()
{
//removed for brevity
serviceX.DoWork(appConfig.SomeAppProp);
Assert.NotNull(result);
Assert.NotEmpty(result);
}
}
Base class (I have a feeling this is the incorrect way of creating the hostbuiler):
public class BaseTest
{
public BaseTest()
{
var builder = CreateHostBuilder();
Task.Run(() => builder.RunConsoleAsync());
}
public static IHostBuilder CreateHostBuilder(string[] args = null) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddJsonFile("appsettings.json", optional: true);
config.AddEnvironmentVariables();
if (args != null)
{
config.AddCommandLine(args);
}
})
.ConfigureServices((hostContext, services) =>
{
services.AddOptions();
services.Configure<AppConfiguration>(hostContext.Configuration.GetSection("AppConfiguration"));
services.AddTransient<IServiceX, ServiceX>();
services.AddTransient<ITemplateEngine, TemplateEngine>();
//removed for brevity..
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
});
}
Ideally, I am looking for a single point where I can setup the HostBuilder
i.e. in a console or API .NET project, this would be in the Main
method of the Program
class.