Telerik JustMock?下载30天免费试用

模拟LINQ查询

本文提供了一些实际的示例,演示如何模拟LINQ查询Telerik®JustMock和自定义选择。

此功能仅在Telerik JustMock的商业版本中可用。指主题了解更多关于Telerik JustMock的商业版本和免费版本之间的区别。

如果您需要一个完整的Visual Studio项目来演示如何模拟LINQ查询,请参考我们的演示。默认的安装目录是C:\Program Files (x86)\Progress\Telerik JustMock\Examples。

先决条件

在进一步的示例中,我们将使用以下方法和示例类进行测试:

公共类SimpleData{公共ExtendedQuery Products {get{返回null;}} public ExtendedQuery Categories {get {return null;}}公共产品GetProduct(int id){返回this.Products。Where(x => x. productid == id).FirstOrDefault();}}公共抽象类ExtendedQuery: IEnumerable {public IEnumerator GetEnumerator(){抛出新的System.NotImplementedException();} System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator(){抛出新的System.NotImplementedException();}}公共类Product{公共int ProductID {get;设置;}公共int CategoryID {get;设置;}公共字符串ProductName {get; set; } public int UnitsInStock { get; set; } public string QuantityPerUnit { get; set; } public bool Discontinued { get; set; } public int voa_class { get; set; } public int GetId() { return this.ProductID; } } public class Category { public int CategoryID { get; set; } public string CategoryName { get; set; } } private List GetProductLists() { var productList = new List() { new Product() { ProductID = 1, CategoryID = 1, ProductName = "test product", UnitsInStock = 3, QuantityPerUnit = "1", Discontinued = false, voa_class = 1 }, new Product() { ProductID = 2, CategoryID = 1, ProductName = "Foo stuff", UnitsInStock = 50, QuantityPerUnit = "1", Discontinued = true, voa_class = 1 }, new Product() { ProductID = 3, CategoryID = 2, ProductName = "More Stuff", UnitsInStock = 0, QuantityPerUnit = "1", Discontinued = true, voa_class = 1 } }; return productList; } private List GetCategoriesLists() { var categoriesList = new List() { new Category() { CategoryID = 1, CategoryName = "First" }, new Category() { CategoryID = 2, CategoryName = "Second" } }; return categoriesList; }

重要的

要模拟LINQ查询,首先需要从菜单中启用TelerikJustMock,从而进入提升模式。学习如何在如何启用/禁用Telerik JustMock的话题。

断言自定义选择查询

在下面的例子中,我们进行模拟简单。产品返回自定义集合。之后,对集合执行LINQ查询以获得一个特定的期望值:

[TestMethod]公共无效ShouldAssertCustomSelect() {var simple =新SimpleData();Mock.Arrange(() => simple.Products).ReturnsCollection(GetProductLists());Var期望=(从简单的p。产品where p.UnitsInStock == 50 select p.ProductID).SingleOrDefault(); Assert.AreEqual(expected, 2); }
作为简单。产品将返回?定义的集合GetProductLists,产品unitsinstocks属性设置为50ProductID设置为2,因此断言将通过。

断言投影

让我们扩展前面的示例。在这里,在我们的LINQ查询中,我们不直接获得我们需要的值,而是构造一个类型的新对象产品验证ProductID财产。

[TestMethod]公共无效ShouldAssertProjectionWhenCombinedWithWhere() {var simple =新SimpleData();Mock.Arrange(() => simple.Products).ReturnsCollection(GetProductLists());Var期望=(从简单的p。产品where p.UnitsInStock == 50 select new { p.ProductID, p.ProductName }).SingleOrDefault(); Assert.AreEqual(expected.ProductID, 2); }

只有一个产品返回的集合中GetProductLists ()unitsinstocks50和它的ProductID财产收益2

使用连接操作符断言查询

此外,您可以使用Join操作在数据源中没有显式建模的序列之间创建关联。例如,您可以执行连接来查找CategoryName每种产品的。

[TestMethod]公共无效ShouldAssertUsingJoinClause() {var simple = new SimpleData();Mock.Arrange(() => simple.Products).ReturnsCollection(GetProductLists());Mock.Arrange(() => simple.Categories).ReturnsCollection(getcategoreslists ());Var查询= from product in simple。产品加入类别简单。产品分类。CategoryID = category。select category.CategoryName;断言。AreEqual (3, query.Count ());}

断言可枚举源

使用JustMock,您还可以模拟可枚举源。让我们添加以下方法和一个类:

public class MyDataContext: DataContext {public MyDataContext(): base(string.Empty) {} public Table Products {get {return this. getttable ();}}} private IQueryable GetFakeProducts() {List productList = GetProductLists();返回productList.AsQueryable ();}

我们安排dataContext。产品方法中定义的集合GetFakeProducts方法。但是它实现了这个IQueryable接口,则允许在断言中使用它。这里我们验证它包含3.元素。

IList <产品>产品;var dataContext = new MyDataContext();Mock.Arrange(() => dataContext.Products).ReturnsCollection(GetFakeProducts());products = dataContext.Products.ToList();断言。AreEqual (3, products.Count ());

当条件表达式作为参数传入时的断言

您还可以在使用LINQ表达式时模拟参数。让我们通过一个示例演示如何模拟的行为= =表达式:

var simple = new SimpleData();int targetProductId = 2;int expectedId = 10;模拟。安排(()=>简单。产品)。return (GetProductLists());模拟。Arrange(() => simple.Products。Where(x => x.c productid == targetProductId). first (). getid())。返回(expectedId). mustcall ();产品实际=简单。GetProduct( targetProductId ); Assert.AreEqual( expectedId, actual.GetId() );

在本例中,我们首先定义产品集合必须是一个特定的产品列表。然后使用LINQ从对象中选择特定实例简单。产品收集。对于该实例,我们希望模拟对GetId ()方法,以便返回不同的id。最后,我们检索目标产品并验证返回的id和期望的id是否相同。

在本文中
Baidu
map