NUnit. Values ​​differ by index [0]

I run the test, but if it fails, I don’t know why:

Proj.Tests.StatTests.GetResults_RegularPage_ReturnListOfResults: Expected and actual are both <System.Collections.Generic.List`1[Proj.Classes.StatResult]> with 50 elements Values differ at index [0] Expected: <test;98318> But was: <test;98318> 

As you can see, the values ​​are identical. Here is the code:

 public class StatResult { public string word { get; set; } public UInt64 views { get; set; } public override string ToString() { return String.Format("{0};{1}", word, views); } } [Test] public void GetResults_RegularPage_ReturnListOfResults() { // Arrange WordStat instance = new WordStat(Constants.WordStatRegularPage); // Act List<StatResult> results = instance.GetResults(); // Assert Assert.AreEqual(results, new List<StatResult> { new WordStatResult { word ="test", views = 98318}, new WordStatResult { word ="test board", views = 7801}, //... I shorted it } 

}

I tried many ways, even putting the test sample directly in the class, but it still doesn't work. Please, help!

+6
source share
3 answers

I see that the two links refer to objects with equal properties, but this is not what is tested here. It checks whether they are references to the same object or that they are equal. Your StatResult class StatResult not override Equals / GetHashCode , so two objects with the same values ​​will be considered "different" for testing.

You must override Equals and GetHashCode so that the two objects are considered equal, respectively. I also suggest making the type immutable, and also following the normal .NET naming conventions for properties:

 public sealed class StatResult : IEquatable<StatResult> { public string Word { get; private set; } public UInt64 Views { get; private set; } public StatResult(string word, ulong views) { this.word = word; this.views = views; } public override string ToString() { return String.Format("{0};{1}", word, views); } public override int GetHashCode() { int hash = 23; hash = hash * 31 + Word == null ? 0 : Word.GetHashCode(); hash = hash * 31 + Views.GetHashCode(); return hash; } public override bool Equals(object other) { return Equals(other as StatResult); } public bool Equals(StatResult other) { return other != null && this.Word == other.Word && this.Views == other.Views; } } 

Your design will simply change to:

 new StatResult("test", 98318), new StatResult("test board", 7801), 

(as well as in your production code).

+5
source

The problem is how NUnit checks the equality of two instances of StatResult . Since you do not implement any form of the equality comparison operator, the default equality check corresponds to the instance reference. Since they are two different instances, their references are different.

For more information on implementing equality in your objects, see this article .

+7
source

You need to override Equals () and GetHashCode (). He is currently checking to see if the first item in each list is a reference to the same object.

+1
source

Source: https://habr.com/ru/post/910601/


All Articles