A source generator for creating mocks and fixtures for testing. SweetMock automatically generates fixture and mock implementations of interfaces or classes to help in unit testing by providing a way to simulate the behavior of complex dependencies.
[Fixture<ShoppingBasket>]
[Fact]
public async Task TheGuideShouldAlwaysBeAvailable()
{
// Arrange
var fixture = Fixture.ShoppingBasket(config =>
{
config.bookRepo.GetByISBN(new Book("isbn 0-434-00348-4", "The Hitch Hiker's Guide to the Galaxy", "Douglas Adams"));
config.messageBroker.SendMessage();
});
var sut = fixture.CreateShoppingBasket();
// Act
await sut.AddBookToBasket("isbn 0-434-00348-4", CancellationToken.None);
// Assert
var sendMessage = Assert.Single(fixture.Log.IMessageBroker().SendMessage());
Assert.Equal("The book The Hitch Hiker's Guide to the Galaxy by Douglas Adams was added to your basket", sendMessage.message);
}Install the package via NuGet:
dotnet add package SweetMockSweetMock offers a comprehensive set of features designed to streamline unit testing in C# projects. Its source generator code simplifies security scanning and ensures that mock implementations are both fast, reliable and maintainable.
The fixture system enables bulk creation of mock dependencies, making it easy to set up complex test scenarios while direct access to creating individual mock object allows for more advanced scenarios.
SweetMock supports both interface and class mocking, providing flexibility for a wide range of dependency types. Built-in mocks cover common scenarios, reducing the need for custom implementations.
Extensive logging features allow for detailed assertions, helping you verify interactions and outcomes with precision.
Additionally, SweetMock is test framework agnostic, working seamlessly with XUnit, NUnit, TUnit, and other popular frameworks.
- C# 12.0 (.net 8 and above)
This project is licensed under MIT License