Alex
Alex

Reputation: 1160

How to inject configuration settings in test classes (ASP.NET Core)?

SmtpConfig contains my credentials which I want to use in a test class. appsettings.development.json

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "SmtpConfig": {
    "credentials": "username:password"
  }
}

Here I configure the smtpConfig to be injected in classes (in controller classes works very fine!) Startup.cs

public IConfigurationRoot Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
      services.AddMvc();

      services.Configure<SmtpConfig(
         Configuration.GetSection(nameof(SmtpConfig)
      ));
}

I want to access credentials from appsettings.development.json in tests, because on another server I will have another config file.

//important usings
using Microsoft.Extensions.Options;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
    public class SomeControllerAPITest
    {
       private SmtpConfig _smtpConfig;

        public SomeControllerAPITest(IOptions<SmtpConfig> smtpConfig)
        {
            _smtpConfig = smtpConfig.Value;
        }


        [TestMethod]
        public void Post_ReturnsCreatedInstance()
        {
            var credentials = _smtpConfig.credentials;

            //use that credentials
            ...

            //call remote server
            ...
        }
}

Is it possible to do that?

Upvotes: 6

Views: 7276

Answers (2)

Jonathan Troncoso
Jonathan Troncoso

Reputation: 31

- Create class file into testProject

public static IConfiguration getConfig(){ 
   var config = new ConfigurationBuilder() 
     .SetBasePath("/Users/Project/")
     .AddJsonFile("appsettings.json")  
     .Build(); 
   return config; 
}

    [TestClass]
    public class TestMasterClass
    {
        public static IConfiguration _configuration { get; set; }

        public TestMasterClass()
        {
            _configuration = AnotherClassFile.getConfig();
        }

        [TestMethod]
        public void TestConfigElasticSearch()
        {

            var elasticSearch = _configuration["ElasticSearchConfig:Link01"];
            Assert.IsNotNull(elasticSearch);
        }
    }

Upvotes: 3

Technetium
Technetium

Reputation: 6188

You can use the same Microsoft.Extensions.Configuration binding functionality to build an identically populated IOptions<TConfiguration> instance. Here is a rough equivalent of how we implemented this for our test code:

public class TestSmtpConfigOptions : IOptions<SmtpConfig> {

    private static Lazy<SmtpConfig> configuration { get; }

    static TestSmtpConfigOptions() {
        configuration = new Lazy<SmtpConfig>(GetConfiguration);
    }

    public SmtpConfig Value {
        get { return configuration.Value; }
    }

    private static SmtpConfig GetConfiguration() {
        var configuration = new SmtpConfig();
        var path = Path.Combine("config", "appsettings.development.json");

        new ConfigurationBuilder()
            .SetBasePath("path/to/base/directory/of/project")
            .AddJsonFile(path, optional: true)
            .Build()
            .GetSection(nameof(SmtpConfig))
            .Bind(configuration);

        return configuration;
    }

}

Then, in your fixture, you only need to instantiate it:

[TestClass]
public class SomeControllerAPITest {

    private SmtpConfig _smtpConfig;

    public SomeControllerAPITest() {
        _smtpConfig = new TestSmtpConfigOptions().Value;
    }


    [TestMethod]
    public void Post_ReturnsCreatedInstance() {
        var credentials = _smtpConfig.credentials;

        //use that credentials
        ...

        //call remote server
        ...
    }
}

In case you care about cross platform paths and don't mind a little extra complexity, here's a little class we use to get the base path in a cross-platform way for our xUnit test runner. This means we use TestConfiguration.BasePath instead of "path/to/base/directory/of/project" in the example above.

internal static class TestConfiguration {

    internal static string BasePath { get; }

    static TestConfiguration() {
        BasePath = Environment.GetEnvironmentVariable("BASE_DIRECTORY");

        if (BasePath == null) {
            BasePath = AppContext.BaseDirectory;

            // cross-platform equivalent of "../../../../../"
            for (var index = 0; index < 5; index++) {
                BasePath = Directory.GetParent(BasePath).FullName;
            }
        }
    }

    internal static string ResolvePath(string relativePath) {
        return Path.Combine(BasePath, relativePath);
    }

}

Upvotes: 0

Related Questions