X
X
x8372013-12-06 09:06:10
.NET
x837, 2013-12-06 09:06:10

Displaying object properties in NUnit

Using NUnit I create a test to compare two arrays:

Class1[] array1;
Class1[] array2;
...
Assert.That(array1, Is.EqualTo(array2).Using(new Class1Comparer()));

As a result, I get the message:
Tester.Test_2861:
Expected and actual are both <Class1[161]> Values ​​differ at index [0]
Expected: <Class1>
But was: <Class1>

How to display clear values ​​instead of "Expected: <Class1>" and "But was: <Class1>"? For example:
Expected: {X: 10, Y: "Hi"}
But was: {X: 10, Y: "Hello"}

The Class1 type is foreign and cannot be changed.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Glebov, 2013-12-09
@x837

The message displays the text returned by ToString().
If Class1 cannot be changed, then you can create a "wrapper" class in which you can override the ToString() method so that it displays the necessary information. You will also have to use another class instead of Class1Comparer that will compare "wrappers"

class Class1Wrapper {
   public readonly Class1 Wrapped;
  
   public Class1Wrapper(Class1 toWrap) { 
       Wrapperd = toWrap;
   }

  public override string ToString() {
      return string.Format("X: {0}, Y : {1}", Wrapped.X, Wrapped.Y);
  }

 public static Class1Wrapper Wrap(Class1 toWrap) { 
    return new Class1Wrapper(toWrap) ;
  }
}

Дальше в тесте

Assert.That(templateSeries.getElements().Select(Class1Wrapper.Wrap).ToArray(),
         Is.EqualTo(resultSeries.getElements().Select(Class1Wrapper.Wrap).ToArray())
          .Using(new Class1WrapperComparer()));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question