SlowFox.UnitTestMocks.xUnit
0.3.1-CI-20230418-125027
Prefix Reserved
See the version list below for details.
dotnet add package SlowFox.UnitTestMocks.xUnit --version 0.3.1-CI-20230418-125027
NuGet\Install-Package SlowFox.UnitTestMocks.xUnit -Version 0.3.1-CI-20230418-125027
<PackageReference Include="SlowFox.UnitTestMocks.xUnit" Version="0.3.1-CI-20230418-125027" />
paket add SlowFox.UnitTestMocks.xUnit --version 0.3.1-CI-20230418-125027
#r "nuget: SlowFox.UnitTestMocks.xUnit, 0.3.1-CI-20230418-125027"
// Install SlowFox.UnitTestMocks.xUnit as a Cake Addin #addin nuget:?package=SlowFox.UnitTestMocks.xUnit&version=0.3.1-CI-20230418-125027&prerelease // Install SlowFox.UnitTestMocks.xUnit as a Cake Tool #tool nuget:?package=SlowFox.UnitTestMocks.xUnit&version=0.3.1-CI-20230418-125027&prerelease
Introduction
SlowFox is a suite of .NET source generators, aiming to reduce the amount of repetitive code you need to write and maintain.
Source generators incur no run-time cost (as no reflection is involved), because the code is created at build time. Plus, using a supported IDE (like Visual Studio 2019/2022), you can see what's being generated immediately when you save your source code.
There are currently 2 generators available via SlowFox:
- Constructors, for generating constructors and private variables
- UnitTestMocks, (this package), for generating mock objects in unit tests
SlowFox.UnitTestMocks (xUnit)
SlowFox.UnitTestMocks is a generator that creates mock objects (using Moq) for the dependencies of a class that is to be tested.
There are also packages that are designed for xUnit and NUnit and MSTest2.
How do I get it working?
Firstly, choose and install the NuGet package relating to your testing framework:
Framework | Package |
---|---|
xUnit | |
NUnit | |
MSTest2 |
Next, create a new test class, mark it as partial
and apply the InjectMocks
attribute, indicating the class that you're going to be tested:
namespace MySampleProject
{
[SlowFox.InjectMocks(typeof(UserHandler))]
public partial class UserHandlerTests
{
}
}
SlowFox will then generate mock objects for each dependency of the selected class, and provide a Create
method that instantiates a new instance of the selected class with the mock objects used as dependencies:
namespace MySampleProject
{
public partial class UserHandlerTests
{
private Mock<IDatabase> _database;
private Mock<ILogger> _logger;
public UserHandlerTests()
{
_database = new Mock<IDatabase>(MockBehavior.Strict);
_logger = new Mock<ILogger>(MockBehavior.Strict);
}
private UserHandler Create()
{
return new UserHandler(_database.Object, _logger.Object);
}
}
}
You can call Create()
in your tests to get the object to test, and you can reference the mock objects to set up any pre-defined responses, or to perform validation:
namespace MySampleProject
{
[SlowFox.InjectMocks(typeof(UserHandler))]
public partial class UserHandlerTests
{
[Fact]
public void VerifyAddUser()
{
_database
.Setup(p => p.Save(It.IsAny<User>()));
UserHandler reader = Create();
reader.CreateNewUser();
_database
.Verify(p => p.Save(It.IsAny<User>()), Times.Once);
}
}
}
You are able to exclude specific types from being mocked, by using the ExcludeMocks
attribute. Any type specified within this attribute will be added as a parameter on the Create
method, so you can provide a value from within your test:
namespace MySampleProject
{
[SlowFox.InjectMocks(typeof(UserHandler))]
[SlowFox.ExcludeMocks(typeof(ILogger))]
public partial class UserHandlerTests
{
[Fact]
public void VerifyAddUser()
{
_database
.Setup(p => p.Save(It.IsAny<User>()));
ILogger testLogger = BuildTestLogger();
UserHandler reader = Create(testLogger);
reader.CreateNewUser();
_database
.Verify(p => p.Save(It.IsAny<User>()), Times.Once);
}
}
}
Note that types that cannot be mocked (e.g., a static or sealed type) will automatically be excluded from being mocked, and will be treated in the same way as types specified in the
ExcludeMocks
attribute
This generator is compatible with constructors that have been generated using SlowFox.Constructors.
Configuration
Configuration is set in a .editorconfig file.
To configure the generated code to not use underscores for member names, set the skip_underscores
value to true:
[*.cs]
slowfox_generation.unit_test_mocks.xunit.skip_underscores = true
To create the mocks using the Loose
behaviour (instead of the default of Strict
), set the use_loose
value to be true:
[*.cs]
slowfox_generation.unit_test_mocks.xunit.use_loose = true
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 4.0.1)
- Microsoft.CodeAnalysis.CSharp.Workspaces (>= 4.0.1)
- Moq (>= 4.16.1)
- SlowFox.Core (>= 0.3.1-CI-20230418-125027)
- SlowFox.UnitTestMocks.Shared (>= 0.3.1-CI-20230418-125027)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.1 | 114 | 5/3/2024 |
1.0.1-CI-20240502-154026 | 37 | 5/2/2024 |
1.0.0 | 492 | 4/19/2023 |
1.0.0-CI-20230419-075719 | 232 | 4/19/2023 |
0.3.1 | 187 | 4/18/2023 |
0.3.1-CI-20230418-125027 | 172 | 4/18/2023 |
0.3.1-CI-20230418-104341 | 215 | 4/18/2023 |
0.3.0 | 362 | 2/24/2023 |
0.3.0-CI-20230224-125642 | 192 | 2/24/2023 |
0.2.0 | 315 | 5/23/2022 |
0.2.0-CI-20220523-151129 | 213 | 5/23/2022 |