Neovolve.Streamline.NSubstitute
2.4.1
dotnet add package Neovolve.Streamline.NSubstitute --version 2.4.1
NuGet\Install-Package Neovolve.Streamline.NSubstitute -Version 2.4.1
<PackageReference Include="Neovolve.Streamline.NSubstitute" Version="2.4.1" />
paket add Neovolve.Streamline.NSubstitute --version 2.4.1
#r "nuget: Neovolve.Streamline.NSubstitute, 2.4.1"
// Install Neovolve.Streamline.NSubstitute as a Cake Addin #addin nuget:?package=Neovolve.Streamline.NSubstitute&version=2.4.1 // Install Neovolve.Streamline.NSubstitute as a Cake Tool #tool nuget:?package=Neovolve.Streamline.NSubstitute&version=2.4.1
Neovolve.Streamline
Neovolve.Streamline provides NuGet packages to simplify the unit testing setup (Arrange in AAA unit testing) of classes and their dependencies. The Neovolve.Streamline
package provides the base logic to creating a SUT (System Under Test) along with any service dependencies that it requires. Other packages (such as Neovolve.Streamline.NSubstitute
) provide the bridge between Neovolve.Streamline
and another tool that creates the service dependencies.
Package | NuGet |
---|---|
Neovolve.Streamline | |
Neovolve.Streamline.NSubstitute |
Use Case
Consider the following class and the setup required to unit test it.
public class Something
{
public Something(
IFirst first,
ISecond second,
IThird third,
IFourth fourth,
IFifth fifth)
{
}
public void FirstAction()
{
}
public void SecondAction()
{
}
public void ThirdAction()
{
}
public void FourthAction()
{
}
public void FifthAction()
{
}
}
There are five dependencies to this test class and five members to unit test. A test Arrange for any of these unit tests may look something like the following (using NSubstitute as the mocking library).
using NSubstitute;
public class SomethingTests
{
[Fact]
public void FirstActionDoesXYZWhenABC()
{
var first = Substitute.For<IFirst>();
var second = Substitute.For<ISecond>();
var third = Substitute.For<IThird>();
var fourth = Substitute.For<IFourth>();
var fifth = Substitute.For<IFifth>();
// Continue Arrange to configure these service for their behaviours
var sut = new Something(first, second, third, fourth, fifth);
// Act
sut.FirstAction();
// Assert
first.Received().FirstAction();
}
}
If you consider that each one of the actions has five business scenarios to validate, that is at least 25 unit test methods that duplicate the above Arrange code. The Neovolve.Streamline.NSubstitute
package can simplify this by automatically creating both the service depedencies and the SUT itself.
For example, the above Arrange can be reduced to just the following:
using NSubstitute;
public class SomethingTests : Tests<Something>
{
[Fact]
public void FirstActionDoesXYZWhenABC()
{
// Configure these service for their behaviours
// Act
SUT.FirstAction();
// Assert
Service<IFirst>().Received().FirstAction();
}
}
Bring your own service
Sometimes a unit test will have its own specific service that should not be created by the Streamline package. In this case, you can use the Use()
, Use(service)
or Use(service, key)
methods to store your own service instance.
public class MyFirst : IFirst
{
}
using NSubstitute;
public class SomethingTests : Tests<Something>
{
[Fact]
public void FirstActionDoesXYZWhenABC()
{
// This will create the MyFirst service instance as both MyFirst and IFirst
Use<MyFirst>();
// Configure these service for their behaviours
// Act
SUT.FirstAction();
// Assert
Service<IFirst>().Received().FirstAction();
}
}
Advantages
This package brings several advantages.
- Service dependencies are automatically created
- The SUT instance is automatically created with any required service dependencies
- Adding, removing or re-ordering constructor parameters have limited or no impact on existing unit tests
- Services and the SUT are automatically disposed via
IDisposable
and/orIAsyncDisposable
- NOTE: This requires that either the test framework implicitly supports disposal or manual integration between the test framework and disposal is required.
- xUnit implicitly supports
IDisposable
but does not yet supportIAsyncDisposable
until v3 is released. In the meantime, the test class can useIAsyncLifetime
to supportIAsyncDisposable
. See https://github.com/xunit/xunit/issues/2017 for further information.
Examples using NSubstitute
SUT with multiple parameters
SUT with single parameter
SUT with no constructor parameters
SUT with custom services
SUT using keyed services
SUT declared as internal having public interface
SUT declared as internal
Using test class constructor parameters
SUT as partial mock
SUT as full mock
SUT as abstract class
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Neovolve.Streamline (>= 2.4.1)
- NSubstitute (>= 5.1.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 |
---|---|---|
2.4.1 | 964 | 5/10/2024 |
2.4.1-beta0001 | 90 | 5/10/2024 |
2.4.0 | 139 | 4/21/2024 |
2.4.0-beta0002 | 96 | 4/21/2024 |
2.3.1 | 367 | 2/17/2024 |
2.3.1-beta0001 | 96 | 2/17/2024 |
2.3.0 | 116 | 2/17/2024 |
2.2.1-beta0001 | 88 | 2/17/2024 |
2.2.0 | 107 | 2/17/2024 |
2.1.1-beta0001 | 85 | 2/17/2024 |
2.1.0 | 859 | 9/22/2023 |
2.0.1-beta0001 | 94 | 9/22/2023 |
2.0.0 | 202 | 8/28/2023 |
2.0.0-beta0009 | 133 | 8/28/2023 |
2.0.0-beta0008 | 117 | 8/28/2023 |
2.0.0-beta0007 | 110 | 8/28/2023 |
1.3.0 | 452 | 7/27/2023 |
1.2.1-beta0001 | 119 | 7/27/2023 |
1.2.0 | 5,840 | 5/11/2022 |
1.2.0-beta0003 | 158 | 5/11/2022 |
1.1.2 | 1,834 | 4/2/2022 |
1.1.2-beta0001 | 147 | 4/2/2022 |
1.1.1 | 2,361 | 9/18/2021 |
1.1.1-beta0001 | 250 | 9/18/2021 |
1.1.0 | 342 | 9/16/2021 |
1.0.1-beta0003 | 239 | 9/16/2021 |
1.0.1-beta0001 | 244 | 1/19/2021 |
1.0.0 | 801 | 1/18/2021 |
0.1.0-beta0009 | 236 | 1/18/2021 |
0.1.0-beta0005 | 258 | 1/18/2021 |
0.1.0-beta0002 | 280 | 1/17/2021 |