0
[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

4

2 回答 2

1

断言实际上应该通过对两个参数进行小写来进行检查。

IE

Assert.AreEqual(expectedCategories[0].ToLower(),actualCategories[0].ToLower());

于 2013-10-04T13:34:16.820 回答
0

试试这样:

// 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);
}
于 2013-10-04T13:25:37.393 回答