比较两个枚举*类型*是否等效?
在我的应用程序中,我有两个等效的枚举。一个位于 DAL 中,另一个位于服务契约层中。它们具有相同的名称(但位于不同的命名空间中),并且应该具有相同的成员和值。
我想编写一个单元测试来强制执行此操作。到目前为止,我已经得到以下内容:
public static class EnumAssert
{
public static void AreEquivalent(Type x, Type y)
{
// Enum.GetNames and Enum.GetValues return arrays sorted by value.
string[] xNames = Enum.GetNames(x);
string[] yNames = Enum.GetNames(y);
Assert.AreEqual(xNames.Length, yNames.Length);
for (int i = 0; i < xNames.Length; i++)
{
Assert.AreEqual(xNames[i], yNames[i]);
}
// TODO: How to validate that the values match?
}
}
这对于比较名称来说效果很好,但是如何检查值是否也匹配?
(我使用的是 NUnit 2.4.6,但我认为这适用于任何单元测试框架)
In my application, I have two equivalent enum
s. One lives in the DAL, the other in the service contract layer. They have the same name (but are in different namespaces), and should have the same members and values.
I'd like to write a unit test that enforces this. So far, I've got the following:
public static class EnumAssert
{
public static void AreEquivalent(Type x, Type y)
{
// Enum.GetNames and Enum.GetValues return arrays sorted by value.
string[] xNames = Enum.GetNames(x);
string[] yNames = Enum.GetNames(y);
Assert.AreEqual(xNames.Length, yNames.Length);
for (int i = 0; i < xNames.Length; i++)
{
Assert.AreEqual(xNames[i], yNames[i]);
}
// TODO: How to validate that the values match?
}
}
This works fine for comparing the names, but how do I check that the values match as well?
(I'm using NUnit 2.4.6, but I figure this applies to any unit test framework)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Enum.GetValues:
Enum.GetValues:
我会翻转你检查的方式。从值获取名称比从名称获取值更容易。迭代值并同时检查名称。
I would flip the way you check around. It is easier to get a name from a value instead of a value from a name. Iterate over the values and check the names at the same time.