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

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

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

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

I am ab

5条回答
  •  余生分开走
    2021-01-04 07:07

    You can try alternative ways. For example, you can try to create an instance of ConfigurationBuilder in your test class:

    string projectPath = AppDomain.CurrentDomain.BaseDirectory.Split(new String[] { @"bin\" }, StringSplitOptions.None)[0];
    IConfiguration config = new ConfigurationBuilder()
       .SetBasePath(projectPath)
       .AddJsonFile("config.json")
       .Build();
    

    Note: Please don't forget to add your config.json file to your test project too.

提交回复
热议问题