I have a windows service application created with.NetCore. I want to create some unit tests for service methods.
The startup class looks like this
public class Startup
{
public static IConfiguration Configuration;
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
}
}
The class that needs to be tested
public class ModelConsumer : IModelConsumer
{
private readonly IProcessHandler _processHandler;
public ModelConsumer(IIProcessHandler processHandler)
{
_processHandler = processHandler;
}
public void OnMessageReceived(ModelDto model)
{
var exportFolder = Startup.Configuration["ExportFolder"];
// do actions
}
}
The test class
[TestClass]
public class ModelConsumerTests
{
[TestMethod]
public void OnMessageReceived()
{
var processHandler = new Mock<IProcessHandler>();
var modelConsumer = new ModelConsumer(processHandler.Object);
var config = InitConfiguration();
var _exportFolder = config["ExportFolder"];
modelConsumer.OnMessageReceived(new ModelDto());
// verify mock call actions
}
public static IConfiguration InitConfiguration()
{
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
return config;
}
}
When I run the test method I receive the following error
There is something that is missing from the configuration, so the test could run successfully?
It's expecting appsettings.json
in the bin/
folder of the Test project. So you'll have to copy it into the test project / make a new one and make sure it gets copied to the output on build.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.