0

I have a method which takes in a list collection of objects. The idea is the method will check a specific field CoverArt in each of the objects passed into it and if the CoverArt property is null, an empty string, or an empty space, it will set it to a default value. If the CoverArt property is not null, empty string, or a white space, it just returns whatever string is in there already.

//checks a list of stories for cover art and sets cover art to a default if it is null
   public string CheckIfCoverArtIsNullForStoryList(List<Story> stories)
   {
    foreach (var story in stories)
    {
        if (story.CoverArt == null || story.CoverArt == "" || story.CoverArt == " ")
    {
    return story.CoverArt = "default-book.png";
   }
   return story.CoverArt;
}

            return "";
        }

What I'm trying to do is create a unit test that will test whether or not this method even works but I'm having difficult trying to compare two lists. What is wrong with my code?

[TestMethod]
        [Description("Tests list of stories for null cover art")]
        public void TestingStoryListCoverArtMethodWithValidInput()
        {
            List<Story> StoryListActual = new List<Story>()
            {
                new Story()
                {
                    CoverArt = "Lord of the Rings.png"
                },
                new Story()
                {
                    CoverArt = "Johnny Appleseed.jpeg"
                },
                new Story()
                {
                    CoverArt = "The Great Gatsby.jpg"
                },
                new Story()
                {
                    CoverArt = "Happy Gilmore.gif"
                }
            };

            List<Story> CoverArtExpected = new List<Story>()
            {
                new Story()
                {
                    CoverArt = "Lord of the Rings.png"
                },
                new Story()
                {
                    CoverArt = "Johnny Appleseed.jpeg"
                },
                new Story()
                {
                    CoverArt = "The Great Gatsby.jpg"
                },
                new Story()
                {
                    CoverArt = "Happy Gilmore.gif"
                }
            };

            helper.CheckIfCoverArtIsNullForStoryList(StoryListActual);

            for (var i = 0; i < StoryListActual.Count; i++)
            {
                for (var j = 0; j < CoverArtExpected.Count; j++)
                {
                    Assert.AreEqual(StoryListActual[i].CoverArt, CoverArtExpected[j].CoverArt);
                }
            }
        }

My unit testing is failing with this message.

enter image description here

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
J.G.Sable
  • 1,258
  • 4
  • 26
  • 62
  • Why do you expect every StoryListActual item to be the same as all CoverArtExpected? Just remove the second for loop – Camilo Terevinto Aug 31 '18 at 23:42
  • because I'm trying to test valid inputs - that none of them should return "default-pic" or whatever. Then I'll try and test when some are empty, null, etc... if I remove the second for loop, how would I get access to each individual member of the second list as they cycle through? – J.G.Sable Aug 31 '18 at 23:44
  • 2
    Your current code says: "every item in StoryListActual is equal to every item in CovertArtExpected." It says "Lord of the Rings.png is equal to Lord of the Rings.png and to Johnny Appleseed.jpeg" – Camilo Terevinto Aug 31 '18 at 23:47
  • You can use [CollectionAssert](https://msdn.microsoft.com/library/ms243763.aspx) to compare lists. – gdir Sep 01 '18 at 00:07
  • Right. I need it to assert that lord of the rings equals lord of the rings, johnny Appleseed jpeg equals Johnny Appleseed jpeg... in another test I’ll put in an empty string and assert that it equal default-pic – J.G.Sable Sep 01 '18 at 00:14
  • 1
    So...? Just use `Assert.AreEqual(StoryListActual[i].CoverArt, CoverArtExpected[i].CoverArt);`? (notice it's `i` in both cases) – Camilo Terevinto Sep 01 '18 at 00:32
  • https://stackoverflow.com/questions/12795882/quickest-way-to-compare-two-list – Chetan Sep 01 '18 at 00:38
  • 1
    Unrelated tip: [string.IsNullOrWhitespace](https://learn.microsoft.com/en-us/dotnet/api/system.string.isnullorwhitespace?view=netframework-4.7.2) method – Richardissimo Sep 01 '18 at 05:26
  • @Richardissimo thanks. I'm aware of it. I'm not taking in a string as a parameter, though, but I could rework the method to do that. – J.G.Sable Sep 01 '18 at 23:50
  • 1
    @J.G.Sable No need to change the parameter I'm suggesting that you do `string.IsNullOrWhitespace(story.CoverArt)`. – Richardissimo Sep 02 '18 at 08:52

0 Answers0