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?
public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } private IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { // TODO: Service configuration code here... } public void Configure(IApplicationBuilder app, ...
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With