C# 如何使用单独测试项目中位于WebAPI项目中的json文件

C# 如何使用单独测试项目中位于WebAPI项目中的json文件,c#,asp.net-core,integration-testing,.net-5,C#,Asp.net Core,Integration Testing,.net 5,TL;DR 我使用第三方库中的枚举,并希望进行一些测试,以检查这些枚举的值是否与位于主web api项目中的字典.postmarks.json文件中的值一致 有没有办法在不将字典.postmarks.json文件复制到测试项目的情况下实现这一点 详细信息: 字典.邮戳.json包含一些密钥对: { "PostMarks": [ { "Code": 0, "Name": "Simple&quo

TL;DR

我使用第三方库中的枚举,并希望进行一些测试,以检查这些枚举的值是否与位于主web api项目中的
字典.postmarks.json
文件中的值一致

有没有办法在不将
字典.postmarks.json
文件复制到测试项目的情况下实现这一点

详细信息:

字典.邮戳.json
包含一些密钥对:

{
  "PostMarks": [
    {
      "Code": 0,
      "Name": "Simple"
    },
    {
      "Code": 1,
      "Name": "Complex"
    },
    {
      "Code": 2,
      "Name": "Any"
    },
    . . .
  ]
}
ConfigureServices
I中,注册选项如下:

var dictionariesConfiguration = new ConfigurationBuilder()
        .SetBasePath(HostEnvironment.ContentRootPath)
        .AddJsonFile("dictionaries.postmarks.json", false, true)
        .Build();

services.Configure<DictionaryOptions>(dictionariesConfiguration);

作为旁注,我有一些使用
WebApplicationFactory
的集成测试,因此我可以从中读取配置,但这是否可行。

您可以将它们作为链接文件添加到项目中。但别忘了将“复制到输出文件夹”设置为“始终”

链接文件只是指向原始文件的指针

public class EnumTests
{
    private readonly IConfiguration _configuration;

    public EnumTests()
    {
        _configuration = InitConfiguration();
    }

    private IConfiguration InitConfiguration()
    {          
        //var dir = should I hard code dir here?

        return new ConfigurationBuilder()
            .SetBasePath(dir)
            .AddJsonFile("dictionaries.postmarks.json")
            .Build();
    }

    [Fact]
    public void PostMarkEnum_ShouldBeEqualToPostMarksOptions()
    {
        var fromOptions = _configuration.GetSection("PostMarks").Get<List<DictionaryElement>>();
        var fromLibrary = Enum.GetValues(typeof(PostMark)).Cast<long>().ToList();

        bool equal = true;

        for (var i = 0; i < fromOptions.Count; i++)
        {
            if (fromOptions[i].Code != fromLibrary[i])
            {
                equal = false;
                break;
            }
        }

        Assert.True(equal);
    }
}

public class DictionaryElement
{
    public long Code { get; set; }
    public string Name { get; set; }
}