SlowFox.UnitTestMocks.NUnit
1.0.0
Prefix Reserved
See the version list below for details.
dotnet add package SlowFox.UnitTestMocks.NUnit --version 1.0.0
NuGet\Install-Package SlowFox.UnitTestMocks.NUnit -Version 1.0.0
<PackageReference Include="SlowFox.UnitTestMocks.NUnit" Version="1.0.0" />
paket add SlowFox.UnitTestMocks.NUnit --version 1.0.0
#r "nuget: SlowFox.UnitTestMocks.NUnit, 1.0.0"
// Install SlowFox.UnitTestMocks.NUnit as a Cake Addin #addin nuget:?package=SlowFox.UnitTestMocks.NUnit&version=1.0.0 // Install SlowFox.UnitTestMocks.NUnit as a Cake Tool #tool nuget:?package=SlowFox.UnitTestMocks.NUnit&version=1.0.0
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 (NUnit)
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
{
[TestFixture]
[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;
[SetUp]
public void Setup()
{
_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
{
[TestFixture]
[SlowFox.InjectMocks(typeof(UserHandler))]
public partial class UserHandlerTests
{
[Test]
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
{
[TestFixture]
[SlowFox.InjectMocks(typeof(UserHandler))]
[SlowFox.ExcludeMocks(typeof(ILogger))]
public partial class UserHandlerTests
{
[Test]
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.nunit.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.nunit.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 (>= 1.0.0)
- SlowFox.UnitTestMocks.Shared (>= 1.0.0)
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 | 480 | 5/3/2024 |
1.0.1-CI-20240502-154026 | 69 | 5/2/2024 |
1.0.0 | 334 | 4/19/2023 |
1.0.0-CI-20230419-075719 | 230 | 4/19/2023 |
0.3.1 | 189 | 4/18/2023 |
0.3.1-CI-20230418-125027 | 168 | 4/18/2023 |
0.3.1-CI-20230418-104341 | 180 | 4/18/2023 |
0.3.0 | 264 | 2/24/2023 |
0.3.0-CI-20230224-125642 | 210 | 2/24/2023 |
0.2.0 | 280 | 5/23/2022 |
0.2.0-CI-20220523-151129 | 254 | 5/23/2022 |