background

Telerik JustMock

Mock LINQ Queries

  • JustMock enables you to easily mock LINQ queries to isolate parts of your code from their dependencies.
  • Part of the fastest, most flexible and complete mocking tool for crafting unit tests.
  • Our award-winning support team is available to assist you with any issues.
Mock everything
  • Mocking LINQ Queries Overview

    Language-integrated queries, also known as LINQ, are a set of technologies based on integrating query capabilities directly into the C# & VB languages. They are very popular and widely used thanks to offering a common syntax for querying any type of data source, decreasing  development time by catching errors at compile-time, etc.

    JustMock enables you to easily mock LINQ queries to isolate parts of your code from their dependencies.
    [TestMethod]
    public void ShouldAssertWithCustomSelect()
    {
        var simpleDataInstance = new SimpleData();
     
        // ARRANGE - When simpleDataInstance.Products_GET is called,
        //           it should return expected collection.
        Mock.Arrange(() => simpleDataInstance.Products)
            .ReturnsCollection(ReturnExpextedCollectionOfProducts());
     
        // ACT - Applying a LINQ query for simpleDataMock.Products.
        var actual = (from p in simpleDataInstance.Products
                      where p.UnitsInStock == 50
                      select p.ProductID).SingleOrDefault();
     
        // ASSERT
        Assert.AreEqual(2, actual);
    }

    Mocking LINQ Queries documentation
Next Steps