liveBook · Manning

Mocking

As explained in .NET MAUI in Action

A mock implementation is a test or development substitute that provides a controlled implementation of an interface or dependency.

Mocking replaces an unavailable dependency with a controlled implementation, allowing UI development and testing to continue before the real functionality exists. section 8.1.1

Use a mock implementation to:

  • Implement an interface with predictable results, such as returning a completed login task with true. section 8.1.1
  • Build and run the app while the real service is unavailable. section 8.1.1
  • Exercise UI behavior and support later unit testing, but not real authenticated API calls. section 8.1.1

Mocking an authentication service

Define IAuthService with LoginAsync returning Task<bool>. Implement MockAuthService, return Task.FromResult(true), store it in LoginPage, and await it when the login button is clicked. Disable the button and show progress while the call runs. section 8.1.1

Listing 8.4 MockAuthService
namespace MauiStockTake.UI.Services;
public class MockAuthService : IAuthService
{
    public Task<bool> LoginAsync() => Task.FromResult(true);
}

Inspect the compact MockAuthService implementation that satisfies IAuthService without contacting a real authentication system. section 8.1.1

How .NET MAUI in Action covers it

How deep it goes
Demonstrates interface definition, a predictable implementation, page integration, and explicit limits around authenticated API behavior.
Examples
Uses MockAuthService, IAuthService.LoginAsync, a login button, progress feedback, and a deterministic successful result.
What it assumes
Assumes basic C# interfaces, classes, Task-based methods, and event handlers while defining the authentication contract explicitly.
What you can do after
Build and test UI flows before implementing the real authentication service.
How it connects
Connects interfaces with authentication services, UI event handling, asynchronous programming, and unit testing.

Also explained in

Compare how 21 books explain mocking →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET MAUI in Action. The section links point to the parts of the book it draws on.