Search code examples
c#enumsxunit

Enum validation


Purpose is to validate Enum.

namespace Test.Enums
{ 
    public enum Type
    { 
        Audi,
        Porsche,
        Peugeot
    }
}

I would like to write these values into an array so that I can compare them.

[Fact]
public void Test()
{
   //arrange
    string[] expected = {"Audi", "Porsche", "Peugeot"};
    string[] actual = new Test.Enums.Type();      

   //assert
    Assert.Equal(expected, actual);
}

How to correctly get enum values into an array to be able to compare them? I will be grateful for your help.


Solution

  • Something like this:

    using System.Linq;
    
    ...
    
    [Fact]
    public void Test()
    {
        string[] expected = {"Audi", "Porsche", "Peugeot"};
        // Enum names
        string[] actual = Enums.GetNames<Test.Enums.Type>();      
    
        // Assert: note the when comparing collections / enumerations
        // we should use SequenceEqual 
        Assert.True(expected.SequenceEqual(actual));
    }