2017-07-05 19 views
4

SmtpConfigには、テストクラスで使用する認証情報が含まれています。 appsettings.development.jsonここテストクラス(ASP.NETコア)に構成設定を挿入する方法は?

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

私はsmtpConfigがクラス内に注入されるように設定(非常にうまく動作し、コントローラクラスで!) Startup.cs

public IConfigurationRoot Configuration { get; } 

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

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

私は、AppSettingsから資格情報にアクセスしたいですテストでは.development.jsonを使用しています。これは、別のサーバで別の設定ファイルがあるためです。

//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 
      ... 
     } 
} 

これは可能ですか?

答えて

0

同じインスタンスを構築する場合は、同じMicrosoft.Extensions.Configurationバインディング機能を使用できます。ここでは、我々のテストコードのためにこれを実装する方法の大まかな同等です:

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; 
    } 

} 

その後は、あなたの固定具に、あなたはそれをインスタンス化する必要があります場合は

[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 
     ... 
    } 
} 

あなたは、クロスプラットフォームのパスを気にもう少し複雑には気にしませんが、xUnitテストランナーのためのクロスプラットフォームの基本パスを得るために使用する小さなクラスです。つまり、上記の例では"path/to/base/directory/of/project"の代わりにTestConfiguration.BasePathを使用します。

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); 
    } 

} 
関連する問題