Mock
Learn to use fake objects (mocks) with Moq.
Mock and Moq
Some classes depend on external services (email, database, clock). Running them for real is slow or impossible. A mock is a fake object that mimics the dependency; Moq is the popular library that creates mocks.
Mock<IEmailService> creates a fake email service. .Setup(...) calls configure behavior; .Object returns an instance that behaves like the real one; at the end .Verify(...) checks whether expected calls were made.
Setup, Object, and Verify
Moq's three core APIs: mock.Setup(s => s.Send(It.IsAny<string>())) defines a return value or side effect for a call; mock.Object gives the fake instance injected into production code; mock.Verify(s => s.Send("x")) checks that a call happened.
This trio answers "if you call this method with this argument, return this" and "did you call this method?". The test thus checks both behavior and the fact of the call.
Exercise Use a Mock with OrderService
Define IEmailService with a void Send(string to) method. OrderService takes an IEmailService in its constructor; its PlaceOrder(string customer) method calls Send(customer) on the service. A Mock<T> class (where T : class) takes a T instance in its constructor, exposes it via Object, and has an empty Setup() method. FakeEmail : IEmailService sets a Called flag in Send. Mark OrderServiceTests.TestPlaceOrderCallsEmail with [Fact]; the body creates a Mock<IEmailService>, calls Setup(), passes mock.Object to OrderService, calls PlaceOrder("Ayşe"), and asserts Assert.True(fake.Called). Main runs the test and prints "Test passed: Mock email".
Show example solution
Define interface IEmailService { void Send(string to); }. Write class OrderService { private readonly IEmailService _email; public OrderService(IEmailService e) => _email = e; public void PlaceOrder(string c) => _email.Send(c); }. Write class Mock<T> where T : class { public T Object { get; } public Mock(T instance) => Object = instance; public void Setup() { } }. Write class FakeEmail : IEmailService { public bool Called; public void Send(string to) => Called = true; }. Define class FactAttribute : Attribute { } and static class Assert { public static void True(bool c) { if (!c) throw new Exception(); } }. Write class OrderServiceTests { [Fact] public void TestPlaceOrderCallsEmail() { var fake = new FakeEmail(); var mock = new Mock<IEmailService>(fake); mock.Setup(); var svc = new OrderService(mock.Object); svc.PlaceOrder("Ayşe"); Assert.True(fake.Called); } }. In Main: var t = new OrderServiceTests(); t.TestPlaceOrderCallsEmail(); Console.WriteLine("Test passed: Mock email");
Example solution:
using System;
interface IEmailService
{
void Send(string to);
}
class OrderService
{
private readonly IEmailService _email;
public OrderService(IEmailService email) => _email = email;
public void PlaceOrder(string customer) => _email.Send(customer);
}
class Mock<T> where T : class
{
public T Object { get; }
public Mock(T instance) => Object = instance;
public void Setup() { }
}
class FakeEmail : IEmailService
{
public bool Called;
public void Send(string to) => Called = true;
}
class FactAttribute : Attribute { }
static class Assert
{
public static void True(bool condition)
{
if (!condition) throw new Exception("Expected: true");
}
}
class OrderServiceTests
{
[Fact]
public void TestPlaceOrderCallsEmail()
{
var fake = new FakeEmail();
var mock = new Mock<IEmailService>(fake);
mock.Setup();
var service = new OrderService(mock.Object);
service.PlaceOrder("Ayşe");
Assert.True(fake.Called);
}
}
class Program
{
static void Main()
{
var tests = new OrderServiceTests();
tests.TestPlaceOrderCallsEmail();
Console.WriteLine("Test passed: Mock email");
}
}
Test yourself
-
Why use a mock?
- To isolate an external dependency
- For faster compilation
- To remove static methods
- To write more tests
Show answer
To isolate an external dependency — Correct. A mock isolates an external dependency, making tests fast and deterministic.
-
Create a mock instance
var mock = new ____<IEmailService>();Show answer
var mock = new Mock<IEmailService>(); -
Match Moq APIs
- Mock<T>
- Creates a fake object
- Setup
- Configures behavior
- Object
- Returns the fake instance
- Verify
- Checks the call