4

I am writing a process that brokers data transfer between two identical APIs (FWIW, one is a local API running in a different container, and the other is in the cloud).

I want to write some test code to ensure that the data transfer code does what it should. Up till now, I've been using TestServer for API tests, but now things might get a little complicated when I want to run two servers. A couple of questions arise:

  1. How do I get them to use different appsettings.json files? Right now, with just one TestServer, it's using the appsettings.json of the test project. How do you configure a TestServer to use a different config file?
  2. Once I have two TestServers up and running, do they function as completely independent entities? Most particularly, if I have any static values, will their values be shared between the two environments, or can they function independently, as they would in real life?
Shaul Behr
  • 36,951
  • 69
  • 249
  • 387

2 Answers2

2
  1. This depends a little bit on how you're creating your TestServer.
    1. If you're using TestServer's constructor (which takes a IWebHostBuilder as input), you can use the web host builder's ConfigureAppConfiguration method to setup whatever configuration sources you want. Appsettings.json is the default config source if you don't change anything, but it should be easy to create two separate TestServers using different IWebHostBuilders, each with their own ConfigureAppConfiguration call.
    2. If you're using the WebApplicationFactory approach, you can create a custom WebApplicationFactory and setup configuration sources in that type's ConfigureWebHost method.
  2. Since they run in the same process/appdomain, the two TestServers will be separate instances but share statics. If that's not desirable, you could consider running on of the servers out-of-process as a 'real' server (instead of a TestServer) or, maybe even better, avoid the use of statics!
MJRousos
  • 381
  • 1
  • 5
0

We need a lot more information here... How are you registering your appsettings.json? Are you using DI?

a) Transform your config for your environment (server/cloud) How do I transform appsettings.json in a .NET Core MVC project?

b) Create a second appsettings and change that in your application config. https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.2

Zakk Diaz
  • 1,063
  • 10
  • 15