title: MSTest: CollectionAssert.AreEquivalent failed. The expected collection contains 1 occurrence(s) of
body: <p><strong>Question</strong>:</p>
  
  <p>Can anyone tell me why my unit test is failing with this error message?</p>
  
  <blockquote>
    <p>CollectionAssert.AreEquivalent failed. The expected collection contains 1
        occurrence(s) of . The actual
        collection contains 0 occurrence(s).</p>
  </blockquote>
  
  <p><strong>Goal</strong>:</p>
  
  <p>I'd like to check if two lists are identical. They are identical if both contain the same elements with the same property values. The order is irrelevant.</p>
  
  <p><strong>Code example</strong>:</p>
  
  <p>This is the code which produces the error. <code>list1</code> and <code>list2</code> are identical, i.e. a copy-paste of each other.</p>
  
  <pre><code>[TestMethod]
  public void TestListOfT()
  {
      var list1 = new List&lt;MyPerson&gt;()
      {
          new MyPerson()
          {
              Name = "A",
              Age = 20
          },
          new MyPerson()
          {
              Name = "B",
              Age = 30
          }
      };
      var list2 = new List&lt;MyPerson&gt;()
      {
          new MyPerson()
          {
              Name = "A",
              Age = 20
          },
          new MyPerson()
          {
              Name = "B",
              Age = 30
          }
      };
  
      CollectionAssert.AreEquivalent(list1.ToList(), list2.ToList());
  }
  
  public class MyPerson
  {
      public string Name { get; set; }
      public int Age { get; set; }
  }
  </code></pre>
  
  <p>I've also tried this line (<a href="https://stackoverflow.com/questions/2441188/mstest-collectionassert-use-with-generics/2441448#2441448">source</a>)</p>
  
  <pre><code>CollectionAssert.AreEquivalent(list1.ToList(), list2.ToList());
  </code></pre>
  
  <p>and this line (<a href="https://stackoverflow.com/questions/662458/unittesting-ilist-with-collectionassert/662474#662474">source</a>)</p>
  
  <pre><code>CollectionAssert.AreEquivalent(list1.ToArray(), list2.ToArray());
  </code></pre>
  
  <p>P.S.</p>
  
  <p>Related Stack Overflow questions:</p>
  
  <p>I've seen both these questions, but the answers didn't help.</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/2441188/mstest-collectionassert-use-with-generics"> CollectionAssert use with generics?</a></li>
  <li><a href="https://stackoverflow.com/questions/662458/unittesting-ilist-with-collectionassert">Unit-testing IList with CollectionAssert</a></li>
  </ul>