C# how to mock Configuration.GetSection(“foo:bar”).Get>()

后端 未结 5 1686
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-04 06:35

I have a list like following in config.json file `

{
  \"foo\": {
    \"bar\": [
      \"1\",
      \"2\",
      \"3\"
    ]
  }
}`

I am ab

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-04 06:52

    Just to add on Ahamed Ishak answer. Convert actual objects to JSON would clean up the code and types are respected. Avoid string typo errors, etc.

            var appSettings = JsonConvert.SerializeObject(new
            {
                Security = new SecurityOptions {Salt = "test"}
            });
    
            var builder = new ConfigurationBuilder();
    
            builder.AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(appSettings)));
    
            var configuration = builder.Build();
    

提交回复
热议问题