XspecT 1.0.0

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

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

XspecT: A fluent unit testing framework

Framework for writing and running automated tests in .Net in a flexible and fluent style, based on the popular "Given-When-Then" pattern, built upon XUnit, Moq, AutoMock and FluentAssertions.

Whether you are beginner or expert in unit-testing, this framework will help you to write more descriptive tests with less code.

Usage

It is assumed that you are already familiar with Xunit and Moq, or similar test and mocking frameworks. This package includes FluentAssertions, but also comes with its own, more limited but less wordy assertion methods, based on the verb Is instead of Should. Is-assertions have the same return-types as Should-assertions, so they can be combined in the same sentence.

Is-assertions are recommended to make the tests read more like specifications, stating facts rather than expectations.

For instance When(SUT.AddNumbers(X, Y)).Given((X, Y) = (1, 2).Then.Result.Is(3)

Test a static method with [Theory]

If you are used to writing one test class per production class and use Theory for test input, you can use a similar style to write minimalistic tests with a minimum of clutter. First you create your test-class, or Specification, overriding StaticSpec<[TReturn]> with the expected return type as generic argument. Then create a test pipeline by calling When with a lambda expression to test. Finally you can verify the result by calling Then on the returned pipeline and check the result with Is

Example:

public class CalculatorSpec : StaticSpec<int>
{
    [Theory]
    [InlineData(1, 1, 2)]
    [InlineData(3, 4, 7)]
    public void WhenAddThenReturnSum(int x, int y, int sum)
        => When(() => Calculator.Add(x, y)).Result.Is(sum);

    [Theory]
    [InlineData(1, 1, 1)]
    [InlineData(3, 4, 12)]
    public void WhenMultiplyThenReturnProduct(int x, int y, int product)
        => When(() => Calculator.Multiply(x, y)).Result.Is(product);
}

For more complex and realistic scenarious, it is recommended to create tests in a separate project from the production code, named [MyProject].Test. The test-project should mimmic the production project's folder structure, but in addition have one folder for each class to test, named as the class. Within that folder, create one test-class per method to test.

Test a static method with [Fact]

  • To test a static method [MyClass].[MyMethod] returning [ReturnType], Create a new abstract class named When[MyMethod] inheriting StaticSpec<[ReturnType]>.

  • Create a parameterless constructor that calls When with a lambda expression calling the method to test: When(() => [MyClass].[MyMethod]([Args...])).

  • Each argument should be defined as a protected field at the top of the class.

  • For each set of preconditions you want to test, Create a nested class within the When-class with the name Given[SomePrecondition] inheriting the outer class.

  • Override Set or call Given to assign values to the arguments of the method-to-test.

  • Finally, create a test method, attributed with [Fact], for each logical assertion you want to make.

  • The assertion should first call Then, which executes the test and returns the result.

  • If there is only one test method in the Given-class, the call to Given can be placed in the chain before Then

  • You can for instance test if a certain value was returned by writing Result.Is([SomeValue]).

Example:

namespace MyProject.Test.Calculator;

public abstract class WhenAddTwoNumbers : StaticSpec<int>
{
    protected int X, Y;
    public WhenAddTwoNumbers() => When(() => MyProject.Calculator.Add(X, Y));

    public class Given_1_And_1 : WhenAdd // Using Set to arrange
    {
        protected override void Set() => (X, Y) = (1, 1);
        [Fact] public void ThenReturn_3() => Result.Is(3);
    }

    public class Given_3_And_4 : WhenAdd // Using Given to arrange
    {
        [Fact] public void ThenReturn_7() => Given(() => (X, Y) = (3, 4)).Result.Is(7);
    }
}

Test a static void method

  • When testing a static void method, there is no return value to verify in result and by convention the generic TResult parameter is set to object.
  • However you can us Throws or NotThrows to verify exceptions thrown.

Example:

namespace MyProject.Test.Validator;

public abstract class WhenVerifyAreEqual : StaticSpec<object>
{
    protected int X, Y;
    protected WhenVerifyAreEqual() => When(() => MyProject.Validator.VerifyAreEqual(X, Y));

    public class Given_1_And_2 : WhenVerifyAreEqual
    {
        [Fact] public void ThenThrows_NotEqual() => Given(() => (X, Y) = (1, 2)).Then.Throws<NotEqual>();
    }

    public class Given_2_And_2 : WhenVerifyAreEqual
    {
        [Fact] public void ThenDoNotThrow() => Given(() => (X, Y) = (2, 2)).Then.NotThrows();
    }
}

Test a class with dependencies

  • To test an instance method [MyClass].[MyMethod], inherit XspecT.SubjectSpec<[MyClass], TResult>.

  • It is recommended practice to create a common baseclass for all tests of [MyClass], named [MyClass]Spec.

  • The subject under test (sut) will be created automatically with mocks and default values by AutoMock. You can supply you own constructor arguments by calling Using (which will be applied in the same order when test pipeline is executed).

  • For each method to test, create an abstract class named When[MyMethod] inheriting [MyClass]Spec in the same way as for static methods.

  • To mock behaviour of any dependency, either override Setup or provide the mocking by calling Given. Each call to Given will provide additional arrangement that will be applied on test execution on the inversed order.

  • The framework gives you direct access to one (lazily generated) mock each of any class type type. You can access a mock by The<MyMockedInterface>().

  • To verify a call to a dependency, write Then.Does<MyMockedInterface>([SomeLambdaExpression]).

  • Moq framework is used to express both mocking and verification of behaviour.

Example:

namespace MyProject.Test.ShoppingService;

public abstract class ShoppingServiceSpec<TResult> : SubjectSpec<MyProject.ShoppingService, TResult>
{
    protected ShoppingServiceSpec() => Using(new MyTestLogger());
}

public abstract class WhenPlaceOrder : ShoppingServiceSpec<object>
{
    protected ShoppingCart Cart;

    protected WhenPlaceOrder() 
        => When(() => SUT.PlaceOrder(Cart)).Given(() => The<ICartRepository>().ReturnsDefault(Cart));

    public class GivenCart : WhenPlaceOrder
    {
        [Fact] public void ThenOrderIsCreated() 
            => Given(() => Cart = new()).Then.Does<IOrderService>(_ => _.CreateOrder(Cart));
    }
}

Test async methods

All the examples above also works for async methods, with small modifications.

More examples can be found as Unit tests in the source code.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows 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
17.2.1 171 1/21/2025
17.2.0 74 1/18/2025
17.1.5 184 11/10/2024
17.1.4 95 11/10/2024
17.1.3 92 11/3/2024
17.1.2 91 11/2/2024
17.1.1 189 10/29/2024
17.1.0 103 10/28/2024
17.0.3 136 10/14/2024
17.0.2 96 10/14/2024
17.0.1 97 10/14/2024
17.0.0 102 10/12/2024
17.0.0-pre.3 56 10/8/2024
17.0.0-pre.2 50 10/6/2024
17.0.0-pre.1 64 10/6/2024
16.4.1 103 10/5/2024
16.4.0 121 10/4/2024
16.3.1 96 9/22/2024
16.3.0 103 9/22/2024
16.2.1 237 9/14/2024
16.2.0 107 9/14/2024
16.1.5 105 9/7/2024
16.1.4 109 9/7/2024
16.1.3 204 9/7/2024
16.1.2 97 9/6/2024
16.1.1 109 9/3/2024
16.1.0 107 9/2/2024
16.0.4 332 8/18/2024
16.0.4-preview 120 8/18/2024
16.0.3-preview 115 8/18/2024
16.0.2-preview 116 8/17/2024
16.0.1-preview 116 8/17/2024
16.0.0-preview 112 8/16/2024
15.7.0 201 8/7/2024
15.6.2 90 7/29/2024
15.6.1 205 7/14/2024
15.6.0 101 7/13/2024
15.5.4 164 7/7/2024
15.5.3 117 7/7/2024
15.5.2 112 7/7/2024
15.5.1 112 7/2/2024
15.5.0 116 6/30/2024
15.4.1 112 6/29/2024
15.4.0 133 6/24/2024
15.3.2 106 6/24/2024
15.3.1 124 6/23/2024
15.3.0 126 6/23/2024
15.2.1 132 6/20/2024
15.2.0 129 6/19/2024
15.1.3-preview 105 6/19/2024
15.1.2 123 6/18/2024
15.1.1 136 6/17/2024
15.1.0 136 6/16/2024
15.0.1 119 6/15/2024
15.0.0 121 6/9/2024
14.2.1 115 6/6/2024
14.2.0 110 6/6/2024
14.1.0 120 5/13/2024
14.0.0 111 5/9/2024
13.3.2 234 4/7/2024
13.3.1 124 1/31/2024
13.3.0 339 1/20/2024
13.2.3 129 1/15/2024
13.2.2 125 1/13/2024
13.2.1 133 1/2/2024
13.2.0 179 1/2/2024
13.1.2 140 12/19/2023
13.1.1 175 12/19/2023
13.1.0 142 12/18/2023
13.0.1 114 12/17/2023
13.0.0 129 12/17/2023
12.2.2 130 12/16/2023
12.2.1 125 12/16/2023
12.2.0 128 12/16/2023
12.1.1 135 12/16/2023
12.1.0 158 12/3/2023
12.0.0 144 12/2/2023
11.0.4 150 11/28/2023
11.0.3 245 11/19/2023
11.0.2 138 11/19/2023
11.0.1 144 11/18/2023
11.0.0 142 11/18/2023
10.0.2 151 11/18/2023
10.0.1 138 11/15/2023
10.0.0 145 11/12/2023
9.3.2 132 11/12/2023
9.3.1 131 11/12/2023
9.3.0 140 11/11/2023
9.2.1 144 11/11/2023
9.2.0 144 11/5/2023
9.1.1 149 10/29/2023
9.1.0 153 10/28/2023
9.0.0 162 10/28/2023
8.5.1 156 10/27/2023
8.5.0 155 10/26/2023
8.4.0 169 10/22/2023
8.3.1 170 10/22/2023
8.3.0 161 10/22/2023
8.2.1 156 10/22/2023
8.2.0 145 10/21/2023
8.1.2 153 10/21/2023
8.1.1 151 10/20/2023
8.1.0 139 10/20/2023
8.0.1 161 10/18/2023
8.0.0 144 10/16/2023
7.2.0 154 10/16/2023
7.1.1 146 10/12/2023
7.1.0 173 10/8/2023
7.0.1 145 10/1/2023
7.0.0 141 10/1/2023
6.4.0 164 9/30/2023
6.3.2 131 9/30/2023
6.3.1 156 9/30/2023
6.3.0 134 9/25/2023
6.2.4 153 9/15/2023
6.2.3 150 9/15/2023
6.2.2 138 9/15/2023
6.2.1 164 9/15/2023
6.2.0 151 9/14/2023
6.1.3 163 9/13/2023
6.1.2 177 9/12/2023
6.1.1 154 9/12/2023
6.1.0 184 9/10/2023
6.0.0 155 9/9/2023
5.5.0 167 9/8/2023
5.4.3 153 9/7/2023
5.4.2 175 9/5/2023
5.4.1 144 9/3/2023
5.4.0 243 8/28/2023
5.3.1 179 8/28/2023
5.3.0 157 8/27/2023
5.2.0 174 8/27/2023
5.1.1 172 8/26/2023
5.1.0 168 8/26/2023
5.0.0 177 8/26/2023
4.5.2 154 8/26/2023
4.5.1 160 8/26/2023
4.5.0 154 8/26/2023
4.4.7 162 8/22/2023
4.4.6 149 8/22/2023
4.4.5 144 8/21/2023
4.4.4 176 8/20/2023
4.4.3 170 8/16/2023
4.4.2 182 8/15/2023
4.4.1 179 8/15/2023
4.4.0 197 8/15/2023
4.3.1 176 8/14/2023
4.3.0 195 8/14/2023
4.2.0 185 8/14/2023
4.1.1 176 8/11/2023
4.1.0 174 8/9/2023
4.0.0 190 8/8/2023
3.3.2 173 8/7/2023
3.3.1 169 8/6/2023
3.3.0 176 8/6/2023
3.2.1 171 8/6/2023
3.2.0 208 8/6/2023
3.1.0 204 8/5/2023
3.0.0 191 8/2/2023
2.4.1 188 8/1/2023
2.4.0 176 8/1/2023
2.3.1 182 7/30/2023
2.3.0 172 7/30/2023
2.2.3 174 7/29/2023
2.2.2 184 7/28/2023
2.2.1 180 7/24/2023
2.2.0 189 7/24/2023
2.1.1 189 7/23/2023
2.1.0 186 7/23/2023
2.0.1 188 7/21/2023
2.0.0 196 7/21/2023
1.1.0 206 7/20/2023
1.0.0 173 7/20/2023

Copied code from deprecated package WhenGivenThen