SlowFox.UnitTestMocks.MSTest 0.3.1-CI-20230418-125027

Prefix Reserved
This is a prerelease version of SlowFox.UnitTestMocks.MSTest.
There is a newer version of this package available.
See the version list below for details.
dotnet add package SlowFox.UnitTestMocks.MSTest --version 0.3.1-CI-20230418-125027                
NuGet\Install-Package SlowFox.UnitTestMocks.MSTest -Version 0.3.1-CI-20230418-125027                
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.MSTest" Version="0.3.1-CI-20230418-125027" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SlowFox.UnitTestMocks.MSTest --version 0.3.1-CI-20230418-125027                
#r "nuget: SlowFox.UnitTestMocks.MSTest, 0.3.1-CI-20230418-125027"                
#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.MSTest as a Cake Addin
#addin nuget:?package=SlowFox.UnitTestMocks.MSTest&version=0.3.1-CI-20230418-125027&prerelease

// Install SlowFox.UnitTestMocks.MSTest as a Cake Tool
#tool nuget:?package=SlowFox.UnitTestMocks.MSTest&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:

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

SlowFox.UnitTestMocks (MSTest2)

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
{
    [TestClass]
    [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;

        [TestInitialize]
        public void Init()
        {
            _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
{
    [TestClass]
    [SlowFox.InjectMocks(typeof(UserHandler))]
    public partial class UserHandlerTests
    {
        [TestMethod]
        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
{
    [TestClass]
    [SlowFox.InjectMocks(typeof(UserHandler))]
    [SlowFox.ExcludeMocks(typeof(ILogger))]
    public partial class UserHandlerTests
    {
        [TestMethod]
        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.mstest.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.mstest.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 93 5/3/2024
1.0.1-CI-20240502-154026 48 5/2/2024
1.0.0 213 4/19/2023
1.0.0-CI-20230419-075719 291 4/19/2023
0.3.1 236 4/18/2023
0.3.1-CI-20230418-125027 262 4/18/2023
0.3.1-CI-20230418-104341 237 4/18/2023
0.3.0 292 2/24/2023
0.3.0-CI-20230224-125642 292 2/24/2023
0.2.0 445 5/23/2022
0.2.0-CI-20220523-151129 320 5/23/2022
0.1.2-CI-20220428-101109 302 4/28/2022