Tutorials ADO.NET Core Tutorial

Mocking Database Calls — Complete Guide

Mocking Database Calls — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of ADO.NET Core Tutorial on Toolliyo Academy.

On this page

ADO.NET Core Tutorial · Lesson 73 of 100

Mocking Database Calls

Foundations ✓SQL & safety ✓ProductionProjects

Production · 3 — ASP.NET & enterprise · ~10 min · Module 8: Testing and Debugging

What is this?

Mock IOrderRepository with Moq/NSubstitute for unit tests; don’t mock SqlConnection internals.

Why should you care?

ShopNest controller tests need mocks, not SQL.

See it live — copy this example

Use a .NET console or API project with SQL Server LocalDB. Run dotnet run after pasting.

var mock = new Mock<IOrderRepository>();
mock.Setup(r => r.GetPendingAsync(It.IsAny<CancellationToken>()))
    .ReturnsAsync(new[] { new OrderDto(1, 5, "Pending") });

What happened?

  • Mock ports, not SqlCommand.
  • Integration tests cover SQL.

Practice next

  1. Moq GetPendingAsync.
  2. Assert controller OK.
  3. Verify call once.
  4. Throw SqlException from mock mapped path.
  5. Null empty list.

Remember

Mock repository ports. Not SqlConnection. Verify interactions.

ShopNest controller mocks

API tests mock IOrderRepository.

Outcome: No SQL needed for HTTP unit tests.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Mid PDF Detailed
SqlCommand: Used for SQL Server databases. It represents a Transact-SQL?
Short answer: command or stored procedure. Example: SqlCommand command = new SqlCommand(&quot;SELECT * FROM Customers&quot;, connection); Example code command or stored procedure. Example: SqlCommand command = new SqlCom…
Mid PDF Detailed
OleDbCommand: Used for databases that support OLE DB providers (e.g., MS?
Short answer: Access, Oracle). Example: OleDbCommand command = new OleDbCommand(&quot;SELECT * FROM Customers&quot;, connection); Example code Access, Oracle). Example: OleDbCommand command = new OleDbCommand(&quot;SELEC…
Mid PDF Detailed
Use a DataAdapter to update the database. Set the InsertCommand,?
Short answer: UpdateCommand, and DeleteCommand properties of the DataAdapter to define how data changes should be applied to the database. Real-world example (ShopNest) Always pass order ids with parameters: cmd.Paramete…
Mid PDF Detailed
OdbcCommand: Used for databases that support ODBC (Open Database?
Short answer: Connectivity) drivers. Example: OdbcCommand command = new OdbcCommand(&quot;SELECT * FROM Customers&quot;, connection); Example code Connectivity) drivers. Example: OdbcCommand command = new OdbcCommand(&qu…
Mid PDF Detailed
Use the DataAdapter's Update() method to push the changes back to the database. Example: SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Customers", connection); SqlCommandBuilder commandBuilder = new SqlCommandBuilder(adapter); // Automatically generates insert, update, delete commands DataSet dataset = new DataSet();
Short answer: dapter.Fill(dataset, &quot;Customers&quot;); // Modify data in the DataSet dataset.Tables[&quot;Customers&quot;].Rows[0][&quot;CustomerName&quot;] = &quot;New Name&quot;; // Update the database with the mod…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

ADO.NET Core Tutorial
Course syllabus

ADO.NET Core Tutorial

Module 1: ADO.NET Fundamentals
Module 2: CRUD Operations
Module 3: Stored Procedures
Module 4: Transactions and Error Handling
Module 5: Performance Optimization
Module 6: ASP.NET Core Integration
Module 7: Advanced Enterprise Topics
Module 8: Testing and Debugging
Module 9: Cloud and DevOps
Module 10: Real-World Enterprise Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details