Mockerade is a modern, strongly-typed mocking library for .NET, powered by source generators. It enables fast, compile-time validated mocks for interfaces and classes, supporting .NET Standard 2.0, .NET 8, .NET 10, and .NET Framework 4.8.
- Source generator-based: No runtime proxy generation, fast and reliable.
- Strongly-typed: Setup and verify mocks with full IntelliSense and compile-time safety.
- Event support: Easily raise and verify events.
- Flexible argument matching: Use
With.Any<T>(),With.Matching<T>(predicate), andWith.Out<T>()for advanced setups.
Install the
Mockeradenuget packagedotnetaddpackageMockerade
Create a mock
usingMockerade;varmock=Mock.For<IMyInterface>();
Set up return values or behaviors for methods and properties on your mock. This allows you to control how the mock responds to calls in your tests.
mock.Setup.AddUser(With.Any<string>()).Returns(newUser(Guid.NewGuid(),"Alice"));mock.Setup.Property.Get().Returns(42);- Use
.Returns(value)to specify the value to return. - You can also set up void methods and property setters.
Mockerade provides flexible argument matching for method setups and verifications:
With.Any<T>(): Matches any value of typeT.With.Matching<T>(predicate): Matches values based on a predicate.With.Out<T>(valueFactory): Matches and sets out parameters.
mock.Setup.AddUser(With.Matching<string>(name =>name.StartsWith("A"))).Returns(newUser(Guid.NewGuid(),"Alicia"));mock.Setup.TryDelete(With.Any<Guid>(),With.Out<User?>(()=>newUser(id,"Alice"))).Returns(true);Easily raise events on your mock to test event handlers in your code:
mock.Raises.UsersChanged(this,EventArgs.Empty);- Use the
Raisesproperty to trigger events declared on the mocked interface or class. - This allows you to simulate notifications and test event-driven logic.
Verify that methods or properties were called with specific arguments and how many times:
mock.Invoked.AddUser("Bob").Invocations.Count();// e.g., 1mock.Invoked.TryDelete(id,With.Out<User?>()).Invocations.Count();- Use the
Invokedproperty to access invocation history for each method or property. - You can assert on the number of invocations or inspect the arguments used.