Benchmarks
These benchmarks measure Mockolate's runtime overhead against Moq, NSubstitute, FakeItEasy, TUnit.Mocks and Imposter for the same end-to-end mocking flow.
- Suite:
Mockolate.Benchmarksdriven by BenchmarkDotNet. - Job:
MediumRunwith the in-process toolchain. - Runner:
ubuntu-latestGitHub Actions agent, refreshed on every push tomain. - Each row shows time and allocated memory, sorted fastest-first; bars are scaled within the row so length corresponds to magnitude. The Mockolate row is highlighted as the baseline (
1.00x) and every other row shows its ratio relative to Mockolate. Lower is better. - Benchmarks parameterised with
[Params(1, 10)](Method/Property/Indexer) repeat their inner body 1× or 10× to surface fixed-cost vs per-call overhead — switch between them with the inline tabs.
- Method
- Property
- Indexer
- Event
- Callback
- Create mock
Setup a method, call it N times with It.IsAny<int>(), then verify it ran exactly N times.
IMyMethodInterface sut = IMyMethodInterface.CreateMock();
sut.Mock.Setup.MyFunc(It.IsAny<int>()).Returns(true);
for (int i = 0; i < N; i++)
sut.MyFunc(42);
sut.Mock.Verify.MyFunc(It.IsAny<int>()).Exactly(N);
Initialise an int property to 42, read+write it N times, then verify the getter and setter each ran exactly N times.
IMyPropertyInterface sut = IMyPropertyInterface.CreateMock();
sut.Mock.Setup.Counter.InitializeWith(42);
for (int i = 0; i < N; i++)
{
_ = sut.Counter;
sut.Counter = i;
}
sut.Mock.Verify.Counter.Got().Exactly(N);
sut.Mock.Verify.Counter.Set(It.IsAny<int>()).Exactly(N);
Setup a string this[int] indexer, read+write it N times, then verify the getter and setter each ran exactly N times.
IMyIndexerInterface sut = IMyIndexerInterface.CreateMock();
sut.Mock.Setup[It.IsAny<int>()].Returns("foo");
for (int i = 0; i < N; i++)
{
_ = sut[42];
sut[42] = "bar";
}
sut.Mock.Verify[It.IsAny<int>()].Got().Exactly(N);
sut.Mock.Verify[It.IsAny<int>()].Set(It.IsAny<string>()).Exactly(N);
Subscribe to a mock event, raise it once, then verify the subscription was recorded.
IMyEventInterface sut = IMyEventInterface.CreateMock();
EventHandler handler = (_, _) => { };
sut.SomeEvent += handler;
sut.Mock.Raise.SomeEvent(null, EventArgs.Empty);
sut.Mock.Verify.SomeEvent.Subscribed().Once();
Configure a callback on a method invocation and trigger it twice — measures the per-invocation cost of side-effect setups.
int count = 0;
IMyCallbackInterface sut = IMyCallbackInterface.CreateMock();
sut.Mock.Setup.MyFunc(It.IsAny<int>()).Do(() => count++);
sut.MyFunc(1);
sut.MyFunc(2);
Cost of constructing an empty mock — no setup, no invocations, no verification. The fixed overhead every test pays.
ICalculatorService.CreateMock();