RotemK.SubstituteUnitTests 1.0.0

dotnet add package RotemK.SubstituteUnitTests --version 1.0.0
NuGet\Install-Package RotemK.SubstituteUnitTests -Version 1.0.0
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="RotemK.SubstituteUnitTests" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RotemK.SubstituteUnitTests --version 1.0.0
#r "nuget: RotemK.SubstituteUnitTests, 1.0.0"
#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 RotemK.SubstituteUnitTests as a Cake Addin
#addin nuget:?package=RotemK.SubstituteUnitTests&version=1.0.0

// Install RotemK.SubstituteUnitTests as a Cake Tool
#tool nuget:?package=RotemK.SubstituteUnitTests&version=1.0.0

SubstituteUnitTests

A simple framework for unit tests using substitutes for dependencies

What is it ?

SubstituteUnitTests aims to reduce the amount of boilerplate code in unit tests.

Consider for example the following code for a unit test using NSubstitute:

[TestMethod]
public void SomeTestMethod()
{
    // Arrange
    var dependencyA = Substitute.For<IDependencyA>();
    // Fake the Work method:
    dependencyA.Work().Returns(35);
    var dependencyB = Substitute.For<IDependencyB>();
    // Additional setup of mocks goes here
    var myClass = new MyClass(dependencyA, dependencyB);
    
    // Act and Assert goes here 
}

Using SubstituteUnitTests we can reduce the code to:

[TestMethod]
public void SomeTestMethod()
{
    // Arrange
    var myClass = CreateUnit(setupHelper =>
            {
                // Fake the Work method:              
                setupHelper.Get<IDependencyA>().Work().Returns(35);
                // Additional setup of mocks goes here
            });
        
    // Act and Assert goes here 
}

How do I use it ?

Your test class should inherit from SubstituteUnitTestBase<T> - The T stands for the class we wish to perform the unit test on.

[TestClass]
public class MyClassTests : SubstituteUnitTestBase<MyClass>
{
...
}

T can be any class that has only interfaces in its ctor or none at all.

Now you can call CreateUnit to create the object of the test: This method will find the most relevant Ctor and initialize it by creating substitutes (using NSubstitute) for each interface parameter.

CreateUnit also accepts an Action<IParameterSetupHelper> delegate so you can setup the mocks for your dependencies:

// The class definition:
public class MyClass
{
    public MyClass(IDependencyA dependencyA, IDependencyB) {...}
}

// Setup the unit test:
CreateUnit(setupHelper =>
{
    // Get ctor parameter by type:              
    setupHelper.Get<IDependencyA>().Work().Returns(35);
    // Get ctor parameter by name:
    setupHelper.Get<IDependencyB>("dependencyB").Work().Returns(49);
});

If you don't want to use the default mocks that are generated, you can set your own values:

// Concrete implementation:
var myDependencyA = new DependencyA();
// Some type of mock:
var myDependencyB = Substitute.For<IDependencyB>();

// Setup the unit test:
CreateUnit(setupHelper =>
{
    // Set ctor parameter by type:              
    setupHelper.Set(myDependencyA);
    // Set ctor parameter by name:
    setupHelper.Set<IDependencyB>("dependencyB");
    // Set ctor parameter and use the return value to continue the setup:
    setupHelper.Set<IDependencyB>("dependencyB").Work().Returns(49);
});
Product Compatible and additional computed target framework versions.
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in 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.0 825 2/9/2019