سؤال

I have some integration tests that get the database connection string from the web.config. My web code and my test code are all in one project and this has worked great until now.

When deployed on app harbor app harbor replaces the value in the web.config, but when in the Visual Studio unit test environment the value is not being pulled.

Is there a way to pull the value from web.config when doing a unit test?

Here is my code:

private static string GetMongoDbConnectionString()
    {
        string con = ConfigurationManager.AppSettings.Get("MONGOHQ_URL") ??
            ConfigurationManager.AppSettings.Get("MONGOLAB_URI") ??
            "mongodb://www.fromCSFile/test";
        return con;
    }

Here is my web.config

<appSettings>
    <add key="MONGOLAB_URI" value="mongodb://www.fromweb.config/test"/>
هل كانت مفيدة؟

المحلول

I wasn't able to get it to work having app harbor inject the correct config settings, but I got close.

I added an app.config to my web project because my web project has my integration tests in it.

I added the environment app setting to app.config:

<appSettings>

<add key="Environment" value="localconfig"/>

I noticed when that code ran on app harbor the environment value was Test:27017.

I wrote this code:

private static string GetMongoDbConnectionString()
{
    string con = ConfigurationManager.AppSettings.Get("MONGOHQ_URL") ??
                ConfigurationManager.AppSettings.Get("MONGOLAB_URI");
    string env = ConfigurationManager.AppSettings.Get("Environment");
    if (env.StartsWith("Test", StringComparison.OrdinalIgnoreCase))
    {
        con = "mongodb://xxxxxxxxx";
    }
    return con;
}

Where the xxxxxxxxx is the value I want to use at integration test time.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top