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 |
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();
}
}
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();
}
}
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 xunit/xunit#2017 for further information.
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