-
Notifications
You must be signed in to change notification settings - Fork 0
Migration Guide
Aryeh Citron edited this page May 12, 2026
·
3 revisions
public class OrderTests : IDisposable
{
private readonly MongoDbRunner _runner;
private readonly IMongoCollection<Order> _collection;
public OrderTests()
{
_runner = MongoDbRunner.Start();
var client = new MongoClient(_runner.ConnectionString);
var database = client.GetDatabase("testdb");
_collection = database.GetCollection<Order>("orders");
}
public void Dispose() => _runner.Dispose();
[Fact]
public async Task Can_insert_and_find()
{
await _collection.InsertOneAsync(new Order { Total = 100 });
var found = await _collection.Find(o => o.Total == 100).FirstOrDefaultAsync();
Assert.NotNull(found);
}
}public class OrderTests
{
private readonly IMongoCollection<Order> _collection;
public OrderTests()
{
var result = InMemoryMongo.Create<Order>("orders");
_collection = result.Collection;
// No runner to start, no connection string
// IMongoClient.Dispose() is a no-op — no cleanup needed
}
[Fact]
public async Task Can_insert_and_find()
{
await _collection.InsertOneAsync(new Order { Total = 100 });
var found = await _collection.Find(o => o.Total == 100).FirstOrDefaultAsync();
Assert.NotNull(found);
}
}-
Remove the
Mongo2GoNuGet package -
Add
InMemoryEmulator.MongoDB -
Replace
MongoDbRunner.Start()+MongoClient(connectionString)withInMemoryMongo.Create<T>() -
Remove
IDisposable/Dispose()for the runner -
Remove any timeout/retry logic for
mongodstartup - Tests should pass without further changes
public class OrderTests : IAsyncLifetime
{
private readonly MongoDbContainer _container;
private IMongoCollection<Order> _collection = null!;
public OrderTests()
{
_container = new MongoDbBuilder()
.WithImage("mongo:7.0")
.Build();
}
public async Task InitializeAsync()
{
await _container.StartAsync();
var client = new MongoClient(_container.GetConnectionString());
_collection = client.GetDatabase("testdb").GetCollection<Order>("orders");
}
public async Task DisposeAsync() => await _container.DisposeAsync();
[Fact]
public async Task Can_insert_and_find()
{
await _collection.InsertOneAsync(new Order { Total = 100 });
var found = await _collection.Find(o => o.Total == 100).FirstOrDefaultAsync();
Assert.NotNull(found);
}
}public class OrderTests
{
private readonly IMongoCollection<Order> _collection;
public OrderTests()
{
_collection = InMemoryMongo.Create<Order>("orders").Collection;
}
[Fact]
public async Task Can_insert_and_find()
{
await _collection.InsertOneAsync(new Order { Total = 100 });
var found = await _collection.Find(o => o.Total == 100).FirstOrDefaultAsync();
Assert.NotNull(found);
}
}-
Remove
Testcontainers.MongoDBNuGet package -
Add
InMemoryEmulator.MongoDB -
Remove the
IAsyncLifetimeimplementation and container setup -
Replace container + client creation with
InMemoryMongo.Create<T>() - Remove Docker dependency from CI configuration
- Tests should pass without further changes
If you're using Moq or NSubstitute to mock IMongoCollection<T>:
var mockCollection = new Mock<IMongoCollection<Order>>();
mockCollection.Setup(c => c.FindAsync(It.IsAny<FilterDefinition<Order>>(), ...))
.ReturnsAsync(mockCursor.Object);
// 50+ lines of setup per test...var collection = InMemoryMongo.Create<Order>("orders").Collection;
// Real query semantics, zero mock setup- Real filter evaluation — your filters are actually tested
-
Real update application —
$set,$inc,$pushactually work - No mock maintenance — refactoring doesn't break test setup
- Integration-level confidence — tests verify actual MongoDB behavior
If your production code uses DI:
// In your test project's WebApplicationFactory or test setup:
services.UseInMemoryMongoDB(options =>
{
options.DatabaseName = "testdb";
options.AddCollection<Order>("orders");
});This replaces all IMongoClient, IMongoDatabase, and IMongoCollection<T> registrations in one line.
- Getting Started — Quick start guide
- Feature Comparison — Detailed feature comparison
- Setup Guide — All DI patterns
Getting Started
Integration & Dependency Injection
Data Management
Reference
- Feature Comparison
- Features
- Filter & Update Operators
- Aggregation Pipeline
- LINQ Support
- API Reference
Help