XspecT 2.4.0
See the version list below for details.
dotnet add package XspecT --version 2.4.0
NuGet\Install-Package XspecT -Version 2.4.0
<PackageReference Include="XspecT" Version="2.4.0" />
paket add XspecT --version 2.4.0
#r "nuget: XspecT, 2.4.0"
// Install XspecT as a Cake Addin #addin nuget:?package=XspecT&version=2.4.0 // Install XspecT as a Cake Tool #tool nuget:?package=XspecT&version=2.4.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, concise and maintainable tests.
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, listing requirements rather than asserting expected results.
This is an example of a complete test class (specification) with one test method (requirement):
using XspecT.Verification;
using XspecT.Fixture;
using static App.Calculator;
namespace App.Test;
public class CalculatorSpec : StaticSpec<int>
{
[Fact] public void WhenAdd_1_and_2_ThenSumIs_3() => Given(1, 2).When(Add).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 with XspecT.
First you create your test-class overriding StaticSpec<[ReturnType]>
with the expected return type as generic argument.
Then create a test-method, attributed with Theory
and InlineData
, called When[Something]
.
This method call Given
and When
, in any order, to setup the test pipeline with test data and the method to test.
Finally verify the result by calling Then.Result
(or only Result
) 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 GivenTwoNumbers_WhenAdd_ReturnSum(int x, int y, int sum)
=> Given(x, y).When(Calculator.Add).Result.Is(sum);
[Theory]
[InlineData(1, 1, 1)]
[InlineData(3, 4, 12)]
public void WhenMultiplyThenReturnProduct(int x, int y, int product)
=> Given(x, y).When(Calculator.Multiply).Result.Is(product);
}
Recommended conventions
For more complex and realistic scenarios, 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 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 use
Throws
orNotThrows
to verify exceptions thrown.
Example:
namespace MyProject.Test.Validator;
public abstract class WhenVerifyAreEqual : StaticSpec<object>
{
protected WhenVerifyAreEqual() => When<int, int>(MyProject.Validator.VerifyAreEqual);
public class Given_1_And_2 : WhenVerifyAreEqual
{
[Fact] public void ThenThrows_NotEqual() => Given(1, 2).Then.Throws<NotEqual>();
}
public class Given_2_And_2 : WhenVerifyAreEqual
{
[Fact] public void ThenDoNotThrow() => Given(2, 2).Then.NotThrows();
}
}
Test a class with dependencies
To test an instance method
[MyClass].[MyMethod]
, inheritXspecT.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 callingGivenThat
. Each call toGivenThat
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 const int ShopId = 2;
//Specify a value to pass to the constructor
protected ShoppingServiceSpec() => Using(ShopId);
}
public abstract class WhenPlaceOrder : ShoppingServiceSpec<object>
{
protected ShoppingCart Cart;
protected WhenPlaceOrder()
=> When(() => SUT.PlaceOrder(Cart.Id)).
GivenThat(() => The<ICartRepository>().Setup(_ => _.GetCart(Cart.Id)).Returns(Cart));
public class GivenCart : WhenPlaceOrder
{
public GivenCart() => GivenThat(() => Cart = new() { Id = 123 });
[Fact] public void ThenOrderIsCreated()
=> Then.Does<IOrderService>(_ => _.CreateOrder(Cart));
[Fact] public void ThenLogsOrderCreated()
=> Then.Does<ILogger>(
_ => _.Information($"OrderCreated from Cart {Cart.Id} in Shop {ShopId}"));
}
}
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 | Versions 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. |
-
net6.0
- AutoFixture (>= 4.18.0)
- FluentAssertions (>= 6.11.0)
- Moq (>= 4.18.4)
- Moq.AutoMock (>= 3.5.0)
- xunit (>= 2.5.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 |
---|---|---|
16.1.1 | 29 | 9/3/2024 |
16.1.0 | 42 | 9/2/2024 |
16.0.4 | 130 | 8/18/2024 |
16.0.4-preview | 69 | 8/18/2024 |
16.0.3-preview | 63 | 8/18/2024 |
16.0.2-preview | 66 | 8/17/2024 |
16.0.1-preview | 69 | 8/17/2024 |
16.0.0-preview | 79 | 8/16/2024 |
15.7.0 | 103 | 8/7/2024 |
15.6.2 | 51 | 7/29/2024 |
15.6.1 | 140 | 7/14/2024 |
15.6.0 | 62 | 7/13/2024 |
15.5.4 | 116 | 7/7/2024 |
15.5.3 | 77 | 7/7/2024 |
15.5.2 | 79 | 7/7/2024 |
15.5.1 | 80 | 7/2/2024 |
15.5.0 | 86 | 6/30/2024 |
15.4.1 | 82 | 6/29/2024 |
15.4.0 | 91 | 6/24/2024 |
15.3.2 | 73 | 6/24/2024 |
15.3.1 | 81 | 6/23/2024 |
15.3.0 | 78 | 6/23/2024 |
15.2.1 | 83 | 6/20/2024 |
15.2.0 | 96 | 6/19/2024 |
15.1.3-preview | 74 | 6/19/2024 |
15.1.2 | 90 | 6/18/2024 |
15.1.1 | 101 | 6/17/2024 |
15.1.0 | 102 | 6/16/2024 |
15.0.1 | 89 | 6/15/2024 |
15.0.0 | 89 | 6/9/2024 |
14.2.1 | 77 | 6/6/2024 |
14.2.0 | 78 | 6/6/2024 |
14.1.0 | 93 | 5/13/2024 |
14.0.0 | 81 | 5/9/2024 |
13.3.2 | 200 | 4/7/2024 |
13.3.1 | 98 | 1/31/2024 |
13.3.0 | 297 | 1/20/2024 |
13.2.3 | 95 | 1/15/2024 |
13.2.2 | 98 | 1/13/2024 |
13.2.1 | 101 | 1/2/2024 |
13.2.0 | 152 | 1/2/2024 |
13.1.2 | 100 | 12/19/2023 |
13.1.1 | 140 | 12/19/2023 |
13.1.0 | 110 | 12/18/2023 |
13.0.1 | 101 | 12/17/2023 |
13.0.0 | 101 | 12/17/2023 |
12.2.2 | 104 | 12/16/2023 |
12.2.1 | 107 | 12/16/2023 |
12.2.0 | 97 | 12/16/2023 |
12.1.1 | 101 | 12/16/2023 |
12.1.0 | 126 | 12/3/2023 |
12.0.0 | 115 | 12/2/2023 |
11.0.4 | 112 | 11/28/2023 |
11.0.3 | 213 | 11/19/2023 |
11.0.2 | 112 | 11/19/2023 |
11.0.1 | 114 | 11/18/2023 |
11.0.0 | 108 | 11/18/2023 |
10.0.2 | 118 | 11/18/2023 |
10.0.1 | 108 | 11/15/2023 |
10.0.0 | 115 | 11/12/2023 |
9.3.2 | 101 | 11/12/2023 |
9.3.1 | 101 | 11/12/2023 |
9.3.0 | 107 | 11/11/2023 |
9.2.1 | 111 | 11/11/2023 |
9.2.0 | 112 | 11/5/2023 |
9.1.1 | 119 | 10/29/2023 |
9.1.0 | 121 | 10/28/2023 |
9.0.0 | 128 | 10/28/2023 |
8.5.1 | 120 | 10/27/2023 |
8.5.0 | 123 | 10/26/2023 |
8.4.0 | 135 | 10/22/2023 |
8.3.1 | 134 | 10/22/2023 |
8.3.0 | 127 | 10/22/2023 |
8.2.1 | 124 | 10/22/2023 |
8.2.0 | 118 | 10/21/2023 |
8.1.2 | 118 | 10/21/2023 |
8.1.1 | 117 | 10/20/2023 |
8.1.0 | 104 | 10/20/2023 |
8.0.1 | 124 | 10/18/2023 |
8.0.0 | 107 | 10/16/2023 |
7.2.0 | 106 | 10/16/2023 |
7.1.1 | 114 | 10/12/2023 |
7.1.0 | 137 | 10/8/2023 |
7.0.1 | 112 | 10/1/2023 |
7.0.0 | 110 | 10/1/2023 |
6.4.0 | 127 | 9/30/2023 |
6.3.2 | 112 | 9/30/2023 |
6.3.1 | 119 | 9/30/2023 |
6.3.0 | 113 | 9/25/2023 |
6.2.4 | 123 | 9/15/2023 |
6.2.3 | 113 | 9/15/2023 |
6.2.2 | 104 | 9/15/2023 |
6.2.1 | 128 | 9/15/2023 |
6.2.0 | 132 | 9/14/2023 |
6.1.3 | 126 | 9/13/2023 |
6.1.2 | 126 | 9/12/2023 |
6.1.1 | 125 | 9/12/2023 |
6.1.0 | 148 | 9/10/2023 |
6.0.0 | 120 | 9/9/2023 |
5.5.0 | 135 | 9/8/2023 |
5.4.3 | 119 | 9/7/2023 |
5.4.2 | 140 | 9/5/2023 |
5.4.1 | 126 | 9/3/2023 |
5.4.0 | 189 | 8/28/2023 |
5.3.1 | 145 | 8/28/2023 |
5.3.0 | 119 | 8/27/2023 |
5.2.0 | 130 | 8/27/2023 |
5.1.1 | 139 | 8/26/2023 |
5.1.0 | 138 | 8/26/2023 |
5.0.0 | 146 | 8/26/2023 |
4.5.2 | 124 | 8/26/2023 |
4.5.1 | 126 | 8/26/2023 |
4.5.0 | 134 | 8/26/2023 |
4.4.7 | 133 | 8/22/2023 |
4.4.6 | 131 | 8/22/2023 |
4.4.5 | 114 | 8/21/2023 |
4.4.4 | 143 | 8/20/2023 |
4.4.3 | 138 | 8/16/2023 |
4.4.2 | 151 | 8/15/2023 |
4.4.1 | 144 | 8/15/2023 |
4.4.0 | 162 | 8/15/2023 |
4.3.1 | 146 | 8/14/2023 |
4.3.0 | 141 | 8/14/2023 |
4.2.0 | 153 | 8/14/2023 |
4.1.1 | 144 | 8/11/2023 |
4.1.0 | 144 | 8/9/2023 |
4.0.0 | 151 | 8/8/2023 |
3.3.2 | 124 | 8/7/2023 |
3.3.1 | 138 | 8/6/2023 |
3.3.0 | 139 | 8/6/2023 |
3.2.1 | 140 | 8/6/2023 |
3.2.0 | 150 | 8/6/2023 |
3.1.0 | 167 | 8/5/2023 |
3.0.0 | 160 | 8/2/2023 |
2.4.1 | 158 | 8/1/2023 |
2.4.0 | 143 | 8/1/2023 |
2.3.1 | 150 | 7/30/2023 |
2.3.0 | 141 | 7/30/2023 |
2.2.3 | 145 | 7/29/2023 |
2.2.2 | 157 | 7/28/2023 |
2.2.1 | 150 | 7/24/2023 |
2.2.0 | 153 | 7/24/2023 |
2.1.1 | 158 | 7/23/2023 |
2.1.0 | 156 | 7/23/2023 |
2.0.1 | 157 | 7/21/2023 |
2.0.0 | 167 | 7/21/2023 |
1.1.0 | 175 | 7/20/2023 |
1.0.0 | 140 | 7/20/2023 |
Use AutoFixture to generate default values for AutoMocker