質問

Something like:

Assert.AreEqual("Id=7, Name=John", someClass.DebuggerInfo);
役に立ちましたか?

解決

I think it's better to override Equals and GetHashCode methods of your SomeClass to compare Id and Name properties:

public class SomeClass
{
    public int Id { get; set; }
    public string Name { get; set; }       

    public override bool Equals(object obj)
    {
        SomeClass other = obj as SomeClass;
        if (other == null)
            return false;

        return other.Id == Id && other.Name == Name;
    }

    // GetHashCode implementation
}

Asserting will look like:

Assert.AreEqual(expectedObject, someClass);

If you don't want or can't change SomeClass implementation, then you can create method which will do assertion:

public void AssertAreEqual(SomeClass expected, SomeClass actual)
{
    Assert.AreEqual(expected.Id, actual.Id);
    Assert.AreEqual(expected.Name, actual.Name);
}

Evaluating debugger display string is not simple task, because DebuggerDisplayAttribute contains only format string, which is used to evaluate string representation of object in debugger. That string besides simple property names can also contain expressions and method calls. You can evaluate debugger display value with help of Roslyn compiler, as described here. But I don't think that usage of debugger metadata is a good way to check for objects equality.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top