SlowFox.UnitTestMocks.xUnit 1.0.1

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
dotnet add package SlowFox.UnitTestMocks.xUnit --version 1.0.1
NuGet\Install-Package SlowFox.UnitTestMocks.xUnit -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SlowFox.UnitTestMocks.xUnit" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SlowFox.UnitTestMocks.xUnit --version 1.0.1
#r "nuget: SlowFox.UnitTestMocks.xUnit, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SlowFox.UnitTestMocks.xUnit as a Cake Addin
#addin nuget:?package=SlowFox.UnitTestMocks.xUnit&version=1.0.1

// Install SlowFox.UnitTestMocks.xUnit as a Cake Tool
#tool nuget:?package=SlowFox.UnitTestMocks.xUnit&version=1.0.1

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:

  1. Constructors, for generating constructors and private variables
  2. UnitTestMocks, (this package), for generating mock objects in unit tests

SlowFox.UnitTestMocks (xUnit)

alternate text is missing from this package README image alternate text is missing from this package README image Build Status Release Date Licence

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 alternate text is missing from this package README image
NUnit alternate text is missing from this package README image
MSTest2 alternate text is missing from this package README image

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
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

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 48 5/3/2024
1.0.1-CI-20240502-154026 27 5/2/2024
1.0.0 369 4/19/2023
1.0.0-CI-20230419-075719 229 4/19/2023
0.3.1 182 4/18/2023
0.3.1-CI-20230418-125027 163 4/18/2023
0.3.1-CI-20230418-104341 213 4/18/2023
0.3.0 347 2/24/2023
0.3.0-CI-20230224-125642 188 2/24/2023
0.2.0 302 5/23/2022
0.2.0-CI-20220523-151129 209 5/23/2022