-2

I am trying to write a unit test to compare actual results to expected results, where the class I am testing gives me a IReadOnlyCollection and my expected results are List.

I read over the this answer about comparing Lists using CollectionAssert at How to compare Lists in Unit Testing, however when I attempt to do so, I get compiler error "cannot convert from 'System.Collections.Generic.IReadOnlyCollection' to 'System.Collections.ICollection'"

Here is my test method:

    [TestMethod]
    public void EveningMotion()
    {
        IDateTimeProvider dateTimeProvider = new MockDateTimeProvider(new DateTime(2018, 1, 1, 20, 0, 0));
        ILightHardwareInterface frontyardLights = new MockLightHardwareInterface();
        MockLightHardwareInterface backyardLights = new MockLightHardwareInterface();

        LightController controller = new LightController(dateTimeProvider, frontyardLights, backyardLights);
        controller.ActuateLights(true);

        IReadOnlyCollection<bool> actualResult = backyardLights.GetRecording();
        List<bool> expectedResult = new List<bool>
        {
            true
        };

        CollectionAssert.AreEqual(expectedResult, actualResult);
    }

What is one to do? I kind of want to keep the expected and actual results comparison rather than iterating through both element by element, if possible.

Christopher Pisz
  • 3,757
  • 4
  • 29
  • 65

1 Answers1

1

You can use CollectionAssert for collections.

CollectionAssert.AreEqual(expectedResult, actualResult);
Django
  • 222
  • 1
  • 5
  • 2
    cannot convert from IReadOnlyCollection to System.Collections.ICollection – Christopher Pisz Jan 02 '18 at 23:07
  • 1
    What is the concrete type of IReadOnlyCollection result? If it is System.Collections.ObjectModel.ReadOnlyCollection, you can cast it to ICollection. – Django Jan 02 '18 at 23:27
  • Problem solved doing it that way. Side question, Is casting via CollectionAssert.AreEqual(expectedResult, (ICollection)actualResult); type safe? I thought it was akin to a reinterpret cast in C++ (I am still a C# noob) and that there would be an "as" method if it was safe. – Christopher Pisz Jan 03 '18 at 16:42