Skip to content

Getting Started

Aryeh Citron edited this page May 12, 2026 · 3 revisions

Getting Started

Installation

dotnet add package InMemoryEmulator.MongoDB

Requirements: .NET 8.0+, MongoDB.Driver 3.0.0+

Optional packages:

Package Purpose
InMemoryEmulator.MongoDB.JsTriggers JavaScript $function, $accumulator, $where via Jint

Quick Start — Integration Tests with Dependency Injection

If your application registers IMongoClient, IMongoDatabase, or IMongoCollection<T> in your DI container, replace them in tests with a single line:

public class MyWebApplicationFactory : WebApplicationFactory<Program>
{
    protected override void ConfigureWebHost(IWebHostBuilder builder)
    {
        builder.ConfigureTestServices(services =>
        {
            services.UseInMemoryMongoDB(options =>
            {
                options.DatabaseName = "testdb";
                options.AddCollection<Order>("orders");
                options.AddCollection<Customer>("customers");
            });
        });
    }
}

All IMongoClient, IMongoDatabase, and IMongoCollection<T> injections will now resolve to in-memory implementations.

Quick Start — Unit Tests (No Dependency Injection)

// Simplest: single collection
var result = InMemoryMongo.Create<Order>("orders");
IMongoCollection<Order> collection = result.Collection;

// Insert, query, update — just like real MongoDB
await collection.InsertOneAsync(new Order { Total = 99.99m });
var orders = await collection.Find(o => o.Total > 50).ToListAsync();

Multiple collections

var mongo = InMemoryMongo.Builder()
    .AddDatabase("mydb", db =>
    {
        db.AddCollection<Order>("orders");
        db.AddCollection<Customer>("customers");
    })
    .Build();

var ordersCollection = mongo.GetDatabase("mydb").GetCollection<Order>("orders");

Seeding Test Data

// Seed in test setup
var result = InMemoryMongo.Create<Product>("products");
await result.Collection.InsertManyAsync(new[]
{
    new Product { Name = "Widget", Price = 9.99m },
    new Product { Name = "Gadget", Price = 19.99m }
});

See Seeding Data for advanced patterns including DI callbacks, file snapshots, and state persistence.

Migrating from Mongo2Go or Testcontainers

Before (Mongo2Go) After (InMemoryEmulator)
var runner = MongoDbRunner.Start(); var result = InMemoryMongo.Create<T>("col");
var client = new MongoClient(runner.ConnectionString); var client = result.Client;
runner.Dispose(); (garbage collected)
Before (Testcontainers) After (InMemoryEmulator)
var container = new MongoDbBuilder().Build(); var result = InMemoryMongo.Create<T>("col");
await container.StartAsync(); (instant, no startup)
var client = new MongoClient(container.GetConnectionString()); var client = result.Client;

See Migration Guide for a detailed walkthrough.

Next Steps

  • Unit Testing — Writing tests with filter builders, update builders, and aggregation
  • How It Works — Understanding the interception architecture
  • Setup Guide — All 5 DI integration patterns
  • Features — Complete feature reference

Clone this wiki locally