枚举验证

发布于 2025-02-06 07:59:11 字数 437 浏览 1 评论 0 原文

目的是验证枚举。

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

我想将这些值写入一个数组,以便我可以比较它们。

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

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

如何将枚举值正确地使枚举值进行比较?我将感谢您的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌辣 2025-02-13 07:59:11

Have a look at Enum.GetNames() which returns a string array containing the enum names.

牵强ㄟ 2025-02-13 07:59:11

这样的事情:

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

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));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文