SweetMock.SourceGenerator
0.9.41
dotnet add package SweetMock.SourceGenerator --version 0.9.41
NuGet\Install-Package SweetMock.SourceGenerator -Version 0.9.41
<PackageReference Include="SweetMock.SourceGenerator" Version="0.9.41"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="SweetMock.SourceGenerator" Version="0.9.41" />
<PackageReference Include="SweetMock.SourceGenerator"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add SweetMock.SourceGenerator --version 0.9.41
#r "nuget: SweetMock.SourceGenerator, 0.9.41"
#:package SweetMock.SourceGenerator@0.9.41
#addin nuget:?package=SweetMock.SourceGenerator&version=0.9.41
#tool nuget:?package=SweetMock.SourceGenerator&version=0.9.41
Sweet Mock
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 userGuid = Guid.NewGuid();
var fixture = Fixture.ShoppingBasket(config =>
{
config.user.Id(userGuid);
var basket1 = Mock.IBasket(c => c.Add());
config.basketRepo
.TryGetUserBasket(Task.FromResult(true), basket1)
.Save();
config.bookRepo
.IsAvailable(true)
.InStock(42)
.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.Calls.messageBroker.SendMessage(arguments => arguments.userId == userGuid));
Assert.Equal("The book The Hitch Hiker's Guide to the Galaxy by Douglas Adams was added to your basket", sendMessage.message);
}
For more details see the documentation
Installation
Install the package via NuGet:
dotnet add package SweetMock
Features
SweetMock 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.
Requirements
- C# 12.0 (.net 8 and above)
License
This project is licensed under MIT License
Project Links
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 5.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on SweetMock.SourceGenerator:
| Package | Downloads |
|---|---|
|
SweetMock
Source generator for creating mocks and fixtures for testing. SweetMock is a tool that automatically generates test fixtures and mock implementations of interfaces or classes helping in unit testing by providing a way to simulate the behavior of complex dependencies. |
GitHub repositories
This package is not used by any popular GitHub repositories.