0

TL;DR

I use enums from 3th party library and I want to have some tests to check that these enums' values are consistent with the values that I have in my dictionaries.postmarks.json file located in the main web api project.

Is there any way to achieve this without copying dictionaries.postmarks.json file to test project?

Details:

dictionaries.postmarks.json contains some keypairs:

{
  "PostMarks": [
    {
      "Code": 0,
      "Name": "Simple"
    },
    {
      "Code": 1,
      "Name": "Complex"
    },
    {
      "Code": 2,
      "Name": "Any"
    },
    . . .
  ]
}

In ConfigureServices I register options as follows:

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

services.Configure<DictionaryOptions>(dictionariesConfiguration);

That's it. From that on I can use IOptions<DictionaryOptions> in my services quite perfectly.

Now I have a separate test project in which I want to check that a 3th party PostMark enum has exactly the same values as I have them in a dictionaries.postmarks.json file.

I don't want to copy json file to my test project (proposed here) because I want to test values that exist exactly in my web api project.

This is what I've come to so far:

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

As a side note, I have some integration test that use WebApplicationFactory, so I could read configurations from it but don't know whether it is possible.

Dmitry Stepanov
  • 2,776
  • 8
  • 29
  • 45

1 Answers1

1

You can add them as linked file in you project. But dont forget to set Copy to output folder to always

Linked files are just a pointer to the original

Smiley
  • 68
  • 6