我有一个接收对象列表集合的方法。想法是该方法将检查传递给它的每个对象中的特定字段CoverArt
,如果CoverArt
属性为null,空字符串或空白,它将设置为默认值。如果CoverArt
属性不为null,空字符串或空格,则它仅返回其中已经存在的任何字符串。
//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 "";
}
我要做的是创建一个单元测试,以测试此方法是否有效,但是我很难比较两个列表。我的代码有什么问题?
[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);
}
}
}
我的单元测试失败,并显示此消息。