1

I have connection strings set up in an app.config file, with different files for debug and release based on this SO answer. I'm setting up an integration test project which should be using a different initial catalog for the tests. How do I get the project, and only the integration test project, to use a different connection string?

Edit: Trying to add some more detail. In app.config I have the following:

In the connection string, it has the line initial catalog=MyProgramDB. However I'd like unit tests to use initial catalog=MyIntegrationTestingDB instead.

I tried creating a separate configuration for testing I can switch to, but I can't change the testing projects to any configuration except Debug or Release.

1 Answers1

0

You didn't specify any code, so I will give you an example of how to initialize it with SQL Server and EF.

In your project Startup, you will need to inject the connection string.

It might be something like this:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<DbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("<Name1>"));
}

The <Name1> will be the connection string that you want for the integration tests. In another project, you might use <Name2>.

Misha Zaslavsky
  • 8,414
  • 11
  • 70
  • 116
  • Where can I put the code snippet you've offered? And I'm not sure what code I can offer; I've edited the question to hopefully add some more details. –  Jul 23 '21 at 08:25
  • Apologies if it was unclear; I'll be honest in that I'm a bit over my head with this. –  Jul 23 '21 at 08:25
  • @HarryAdams You need to put it in Startup.cs file. Probably in your test project, you don't have this file but there are some ways to do it. For example, if you use xUnit you might take a look at https://github.com/pengweiqhca/Xunit.DependencyInjection I would also recommend you to look at this article which is not exactly as I described above but it is something that might help you: https://timdeschryver.dev/blog/how-to-test-your-csharp-web-api#withwebhostbuilder – Misha Zaslavsky Jul 23 '21 at 08:37
  • Sorry, silly question - is this ASP.NET specific? We're using C#/.net framework? The things I'm reading suggest this is ASP specific? –  Jul 23 '21 at 10:10