Easily Perform LINQ Mocking to Unit Test ASP.NET Core Application
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (179).NET Core  (29).NET MAUI  (214)Angular  (110)ASP.NET  (51)ASP.NET Core  (83)ASP.NET MVC  (90)Azure  (41)Black Friday Deal  (1)Blazor  (226)BoldSign  (15)DocIO  (24)Essential JS 2  (109)Essential Studio  (200)File Formats  (68)Flutter  (133)JavaScript  (226)Microsoft  (120)PDF  (81)Python  (1)React  (105)Streamlit  (1)Succinctly series  (131)Syncfusion  (942)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (52)Windows Forms  (61)WinUI  (71)WPF  (163)Xamarin  (161)XlsIO  (38)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (9)Business intelligence  (55)Button  (4)C#  (157)Chart  (140)Chart of the week  (54)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (70)Development  (658)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (7)Essential Tools  (14)Excel  (43)Extensions  (22)File Manager  (7)Gantt  (19)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (512)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (54)Security  (4)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (14)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (393)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (614)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Easily Perform LINQ Mocking in ASP.NET Core Applications

Easily Perform LINQ Mocking to Unit Test ASP.NET Core Application

LINQ (Language-Integrated Query) helps you extract data from XML documents, SQL databases, arrays, and all other third-party data sources with the same basic code patterns. This enables you to avoid writing a separate query for each database.  In this blog post, we are going to see how to perform LINQ mocking in an ASP.NET Core application to perform unit testing.

Mocking is a process involved in the unit testing of applications. Here, we replace (mock) the external dependencies (e.g., data) with pseudo-dependencies to test whether the units of code function as expected.

Likewise, in LINQ mocking, we can mock the entity data used in LINQ queries. In real-time applications, if you write a unit test for the methods that access the real data from a database, even a minor problem in the database will cause a failure in the unit test. At the same time, a real-time database will take on additional load while executing the unit test.

In this blog, we explain how to mock a data collection and the selection process based on LINQ queries.

Let’s explore them!

Method to be tested

We are going to test the following method with LINQ mocking in our ASP.NET Core application. This method should return the customer ID (int) when providing the customer’s email ID as the parameter to the method.

public int GetCustomerIdBasedOnEmail(string customerEmail)
     int customerId = 0;

     if (!string.IsNullOrEmpty(customerEmail))
           using (CustomerEntity context = this.CustomerEntity ?? new CustomerEntity())
               customerId = (from user in context.CustomerData
                     where user.email == customerEmail
                     select user.Id).FirstOrDefault();

           this.CustomerEntity = null;
         catch (Exception ex)

   return customerId;


Follow these steps to perform LINQ mocking in your ASP.NET Core application:

Step 1: Initialize the data

Here, we are going to check how the data has been assigned with the mocked data collection. Note the following:

  1. Mock a specified set of tables alone based on the need of the unit test from the entity you defined. You can skip the irrelevant tables.
  2. Narrow down a specified set of column values to mock based on the query.  You should include only the required columns in the LINQ query in which you are going to perform unit testing.

Entity Framework Core provides support for configuring DbContextOptions. Databases define extension methods on the object that allow you to configure the database connection to be used for a context.

This context will allow you to configure table data to be mocked. Initialization of the table data will be considered as mocked data inserted into that table. Mocked data can vary based on your test cases.

Refer to the following code example.

public class MockCustomerContext<T>
   /// <summary>
   /// Get Customer context mock object
   /// </summary>
   /// <returns>Context object</returns>
   public CustomerEntity GetCustomerContext()
       var options = new DbContextOptionsBuilder<CustomerEntity>()
        var context = new CustomerEntity(options);

        // CustomerData => table name
        context.CustomerData.Add(new CustomerData { Id = 111, email = "test@gmail.com" });

        return context;

Step 2: Call mocked context while creating class object

We need to get the mocked data collection to the method of the class object in which you are going to do unit testing. Also note the following:

  1. Make sure to have an interface which includes your method as well as the property for your entity.
  2. Use a parameterized constructor to get the interface object. In a real-time scenario, you should create a new direct object for the entity (direct entity object will hit the real database) when the interface object is empty. The mocked entity object will hit only from the unit testing application.

Refer to the following code example.

this.ICustomerDataAccess = Substitute.For<ICustomerDataAccess>();
this.ICustomerDataAccess.CustomerEntity = new MockCustomerContext<CustomerEntity>().GetCustomerContext();
this.CustomerDataAccess = new CustomerDataAccess(this.ICustomerDataAccess);

Step 3: Write test cases

Then, we can state the act (action to be performed) and assert (to validate the action) in the test cases to validate the LINQ query. For this, you just need to call the method in Act and Assert as mentioned in the following code example. Here is just a test of whether the object is null.

/// <summary>
/// Valid Customer.
/// </summary>
public void GetCustomerIdBasedOnEmail_ValidCustomerEmail_ReturnCustomerId()
    int customerId = this.CustomerDataAccess.GetCustomerIdBasedOnEmail("test@gmail.com");

    Assert.AreEqual(111, customerId);

Note: You can perform as much testing based on the data you mocked from the collection as you want.

Output screenshot

Here, we used NUnit to test the cases.

Unit test result

GitHub Repository

For more information, refer to the LINQ Mocking in ASP.NET Core application demo.


In this blog post, we have learned how to perform LINQ mocking in an ASP.NET Core application to perform unit testing. Try out the steps provided in this blog and share your feedback in the comments section below.

The Syncfusion ASP.NET Core UI controls library is the only suite that you will ever need to build an application. It contains over 70 high-performance, lightweight, modular, and responsive UI controls in a single package. Use them to enhance your productivity!

In addition to the comment section below, you can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

If you like this blog post, we think you will like the following blogs and ebooks too:


Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed