我是單元測試新手,所以我確信這是一個非常基本的問題,但是當我搜索它時找不到解決方案。Moq單元測試按產品類別過濾產品
我正在測試以查看是否可以按其類別過濾產品。我可以訪問我的Product類中的所有屬性,但不能訪問我的Category類中的所有屬性。例如,它找不到Category1.Name。誰能告訴我我做錯了什麼?
這是我的產品類;
public partial class Product
{
public int ProductID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public int CategoryID { get; set; }
public virtual Category Category1 { get; set; }
}
這是我的測試;
[TestMethod]
public void Can_Filter_Products()
{
//Arrange
Mock<IProductRepository> mock = new Mock<IProductRepository>();
mock.Setup(m => m.Products).Returns(new Product[]
{
new Product {ProductID=1,Name="P1", **Category1.Name** = "test1" },
new Product {ProductID=2,Name="P2", **Category1.Name** = "test2"},
new Product {ProductID=3,Name="P3", **Category1.Name** = "test1"},
new Product {ProductID=4,Name="P4", **Category1.Name** = "test2"},
new Product {ProductID=5,Name="P5", **Category1.Name** = "test3"},
}.AsQueryable());
//Arrange create a controller and make the page size 3 items
ProductController controller = new ProductController(mock.Object);
controller.PageSize = 3;
//Action
Product[] result = ((ProductsListViewModel)controller.List("test2", 1).Model).Products.ToArray();
//Assert - check that the results are the right objects and in the right order.
Assert.AreEqual(result.Length, 2);
Assert.IsTrue(result[0].Name == "P2" && result[0].Category1.Name == "test2");
Assert.IsTrue(result[1].Name == "P4" && result[1].Category1.Name == "test2");
}
什麼是'** ** Category1.Name'怎麼辦呢? – 2013-04-28 02:56:09
這只是我的類別類中的名稱屬性。我有一個導航欄,當選擇一個類別時,它會將該值傳遞給我的控制器,並根據匹配的類別名稱對產品進行過濾。 – MountainBiker 2013-04-28 03:07:27