Microsoft.TestApi对象比较无法识别枚举差异

时间:2016-03-24 09:47:14

标签: c# enums comparison

我尝试使用Microsoft.TestApi比较2个对象。问题是它没有找到枚举值之间的差异。

在下面的示例中,我希望测试通过,因为枚举值不同,但TestApi比较不会返回不匹配。

如何检测枚举值差异?

代码

public class ObjectComparer
{
    public bool AreDifferent(Holiday h1, Holiday h2)
    {
        var comparer = new ObjectGraphComparer();

        var factory = new PublicPropertyObjectGraphFactory();
        var sourceGraph = factory.CreateObjectGraph(h1);
        var targetGraph = factory.CreateObjectGraph(h2);

        IEnumerable<ObjectComparisonMismatch> result;
        comparer.Compare(sourceGraph, targetGraph, out result);

        return result.Any();
    }
}

public class Holiday
{
    public Day Day { get; set; }
}

public enum Day
{
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday
}

测试

[TestMethod]
public void ShouldDetectDifferentEnumValues()
{
    var h1 = new Holiday {Day = Day.Monday};
    var h2 = new Holiday {Day = Day.Tuesday};
    var comparer = new ObjectComparer();

    var result = comparer.AreDifferent(h1, h2);

    Assert.IsTrue(result);
}

1 个答案:

答案 0 :(得分:1)

我复制了您的代码并在源代码中进行了一项小型研究。我的朋友你运气不好,没有比较枚举。

代码可以在codeplex找到,相关部分是这个

// compare primitives, strings, datatimes, guids
if (leftNode.ObjectType.IsPrimitive ||
            leftNode.ObjectType == typeof(string) ||
            leftNode.ObjectType == typeof(DateTime) ||
            leftNode.ObjectType == typeof(Guid))