如何在选择列表中进行比较
本文关键字:比较 列表 选择 | 更新日期: 2023-09-27 18:33:51
[TestMethod]
public void TestCreate() {
// Setup
AdvertisementController controller = new AdvertisementController();
ViewResult result = controller.Create() as ViewResult;
var expectedCategories = new SelectList(new[] { "Electronics", "Toys", "Books", "Sporting Goods" });
// Execute
var actualCategories = result.ViewBag.categoryList;
// Assert
Assert.IsNotNull(result);
Assert.AreEqual(expectedCategories[0], actualCategories[0]);
Assert.AreEqual(expectedCategories[1], actualCategories[2]);
Assert.AreEqual(expectedCategories[2], actualCategories[3]);
Assert.AreEqual(expectedCategories[3], actualCategories[4]);
Assert.AreEqual(expectedCategories[4], actualCategories[5]);
}
此断言无法正常工作。如何比较result.ViewBag
和实际SelectList
?
断言实际上应该通过对两个参数进行小写来检查。
即
Assert.AreEqual(expectCategories[0].ToLower(), actualCategories[0].ToLower());
尝试如下:
// Execute
var actualCategories = (SelectList)result.ViewBag.categoryList;
// Assert
Assert.IsNotNull(result);
Assert.AreEqual(expectedCategories.Count(), actualCategories.Count());
for (var i = 0; i < expectedCategories.Count(); i++)
{
Assert.AreEqual(expectedCategories.ElementAt(i).Value, actualCategories.ElementAt(i).Value);
Assert.AreEqual(expectedCategories.ElementAt(i).Text, actualCategories.ElementAt(i).Text);
}