Is it possible to use Microsoft.VisualStudio.QualityTools.UnitTesting.CollectionAssert for IEnumerable <T>?
I have a testing scenario where I want to check if two collections are equal. I found the Microsoft.VisualStudio.QualityTools.UnitTesting.CollectionAssert class, but it only works on ICollection<T> . Since I am testing the repository for the Entity Framework and therefore I need to compare IObjectSet<T> s, which will not do - IObjectSet<T> does not implement ICollection<T> .
Can I use this class to compare collections, or do I need to create my own implementation? (And why the hell didn't the Microsoft team do the class work with IEnumerable<T> instead, since this is the "base interface" for collections?)
EDIT: This is my test code:
// Arrange var fakeContext = new FakeObjectContext(); var dummies = fakeContext.Dummies; var repo = new EFRepository<DummyEntity>(fakeContext); // Act var result = repo.GetAll(); // Assert Assert.IsNotNull(result, NullErrorMessage(MethodName("GetAll"))); Assert.IsInstanceOfType(result, typeof(IEnumerable<DummyEntity>), IncorrectTypeMessage(MethodName("GetAll"), typeof(IEnumerable<DummyEntity>))); CollectionAssert.AreEqual(dummies.ToList(), result.ToList()); The call to CollectionAssert.AreEqual on the last line failed, indicating that the elements with index 0 are not equal. What am I doing wrong?
An obsessive option (not much information) is to claim that expected.SequenceEqual(actual) returns true .
Can I write a wrapper method that forces a collection ( .ToList() for each)? But honestly, you can just call .ToList() in unit test code:
CollectionAssert.AreEqual(expected.ToList(), actual.ToList()); // but tidier... If you are comparing result sets, you can use CollectionAssert.AreEquivalent , which will ignore the order. You should also make sure that you have implemented Equals on the type of elements you are comparing.