Test mocks for Zig. Call trackers, string spies, return mocks, error mocks — zero dependencies.
Lightweight test doubles for Zig. Track function calls, spy on string arguments, mock return values and errors.
const ziomock = @import("ziomock");
// Track how many times something was called
var tracker: ziomock.CallTracker(10) = .{};
tracker.record();
tracker.record();
try std.testing.expect(tracker.wasCalledN(2));
// Spy on string arguments
var spy: ziomock.StringSpy(10) = .{};
spy.record("hello");
try std.testing.expect(spy.wasCalledWith("hello"));
// Mock a return value
var mock = ziomock.ReturnMock(u32).init(42);
const val = mock.call(); // 42
// Mock errors: fail N times then succeed
var err_mock = ziomock.ErrorMock(u32).init(2, 99);
const result = err_mock.call(); // error.MockError
const result2 = err_mock.call(); // error.MockError
const ok = try err_mock.call(); // 99zig fetch --save git+https://github.com/deblasis/ziomockThen in your build.zig:
const dep = b.dependency("ziomock", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("ziomock", dep.module("ziomock"));Requires Zig 0.16.
CallTracker(max).record()/.callCount()/.wasCalledN(n)StringSpy(max).record(s)/.wasCalledWith(s)ReturnMock(T).init(value)/.call()ErrorMock(T).init(fail_count, value)— fail N times then succeed
- Zig: 0.16.0
- Platforms: Linux, macOS, Windows
- Breaking changes: follows Semantic Versioning. Minor versions add features, patch versions fix bugs.
MIT. Copyright (c) 2026 Alessandro De Blasis.