NoWoL.TestingUtilities
0.1.18
See the version list below for details.
dotnet add package NoWoL.TestingUtilities --version 0.1.18
NuGet\Install-Package NoWoL.TestingUtilities -Version 0.1.18
<PackageReference Include="NoWoL.TestingUtilities" Version="0.1.18" />
paket add NoWoL.TestingUtilities --version 0.1.18
#r "nuget: NoWoL.TestingUtilities, 0.1.18"
// Install NoWoL.TestingUtilities as a Cake Addin #addin nuget:?package=NoWoL.TestingUtilities&version=0.1.18 // Install NoWoL.TestingUtilities as a Cake Tool #tool nuget:?package=NoWoL.TestingUtilities&version=0.1.18
NoWoL.TestingUtilities
NoWoL.TestingUtilities is a collection of testing utilities for C# development. Presently it contains helper methods to help test parameters validation for constructor and public methods.
Installation
You can install the NoWoL.TestingUtilities
packages using your favorite Nuget package manager.
Usage
Before you can test the input arguments of a method or constructor you need to acquire an ArgumentsValidator
. The library provides a helper class called ArgumentsValidatorHelper
to help with the validator creation. Once you have your validator you will need to configure every parameters using the SetupParameter
method and then call Validate
to test the parameters.
// the sample code below will use the following class definition
public class TestClass
{
public TestClass(string param1, IMyInterface param2)
{
if (param1 == null) throw new ArgumentNullException(nameof(param1));
if (String.IsNullOrWhiteSpace(param1)) throw new ArgumentException("Cannot be null, empty or whitespace", nameof(param1));
if (param2 == null) throw new ArgumentNullException(nameof(param2));
}
public void MyMethod(string param1, List<int> param2)
{
if (param2 == null) throw new ArgumentNullException(nameof(param2));
if (param2.Count == 0) throw new ArgumentException("Cannot be empty", nameof(param2));
}
}
// Validates the constructor's parameters
var validator = ArgumentsValidatorHelper.GetConstructorArgumentsValidator<TestClass>();
validator.SetupParameter("param1", ExpectedExceptionRules.NotNull, ExpectedExceptionRules.ExpectedNotEmptyOrWhiteSpaceException) // validates that the string is not null, empty or only white spaces
.SetupParameter("param2", ExpectedExceptionRules.NotNull, ExpectedExceptionRules.NotEmpty) // validates that the list is not null and not empty
.Validate();
// Validates the method's parameters
var obj = new TestClass();
var validator = ArgumentsValidatorHelper.GetMethodArgumentsValidator(obj, nameof(TestClass.MyMethod));
validator.SetupParameter("param1", ExpectedExceptionRules.None) // validates that no exception are thrown for the parameter
.SetupParameter("param2", ExpectedExceptionRules.NotNull, ExpectedExceptionRules.NotEmpty)
.Validate();
It is also possible to test async/await
code using the ValidateAsync
method.
Type creation
The library needs to know how to create the different object to correctly call the methods under test. The most common types are handled by default however you may need to create a type that is not handled by default. To do so, you will need to create an instance of IObjectCreator
to handle the type creation.
public class TestClassObjectCreator : IObjectCreator
{
public bool CanHandle(Type type)
{
if (type == null) throw new ArgumentNullException(nameof(type));
return type == typeof(TestClass);
}
public object Create(Type type, ICollection<IObjectCreator> objectCreators)
{
if (type == null) throw new ArgumentNullException(nameof(type));
if (objectCreators == null) throw new ArgumentNullException(nameof(objectCreators));
if (CanHandle(type))
{
return new TestClass("Text", CreatorHelpers.CreateItemFromType(typeof(IMyInterface))); // interfaces will be handled by the Moq object creator
}
throw new NotSupportedException("Expecting a List<> type however received " + type.FullName);
}
}
// You will need to create a new collection of object creators for your validator:
var creators = new List<IObjectCreator>();
creators.Add(new TestClassObjectCreator()); // you should add your object creators before the default ones
creators.AddRange(ArgumentsValidatorHelper.DefaultCreators); // add the default creators
var validator = ArgumentsValidatorHelper.GetMethodArgumentsValidator(obj,
nameof(AnotherClass.AnotherMethod),
objectCreators: creators);
To avoid creating too many one-off object creators you can simply create the test values yourself and pass them to the validator.
var values = new object[]
{
"firstvalue",
new List<int> { 3 },
...
};
var validator = ArgumentsValidatorHelper.GetMethodArgumentsValidator(obj,
nameof(AnotherClass.AnotherMethod),
methodArguments: values);
...
Exception Validation Rules
The library comes with the most common validation rules for no exception, not empty, not empty or white-space, not null and specific invalid value. The ExpectedExceptionRules
class holds instances of these rules.
You can create your own validation rule by inheriting from IExpectedException
. This class provides 2 main methods: Evaluate
which is used to analyze an exception that was thrown (if any) and GetInvalidParameterValue
which is used to generate and invalid value used as input for the method under test.
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
License
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
- Moq (>= 4.16.1)
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 |
---|---|---|
0.1.32 | 2,036 | 5/11/2022 |
0.1.31 | 423 | 5/7/2022 |
0.1.30 | 828 | 4/10/2022 |
0.1.29 | 440 | 4/10/2022 |
0.1.28 | 449 | 4/10/2022 |
0.1.27 | 450 | 2/1/2022 |
0.1.26 | 282 | 1/6/2022 |
0.1.25 | 277 | 1/6/2022 |
0.1.24 | 282 | 1/6/2022 |
0.1.23 | 282 | 1/4/2022 |
0.1.22 | 260 | 1/4/2022 |
0.1.21 | 293 | 1/4/2022 |
0.1.20 | 268 | 1/3/2022 |
0.1.19 | 289 | 1/3/2022 |
0.1.18 | 301 | 1/2/2022 |
0.1.17 | 269 | 1/2/2022 |
0.1.16 | 451 | 1/2/2022 |
0.1.15 | 278 | 1/2/2022 |
0.1.14 | 264 | 1/1/2022 |
0.1.12 | 286 | 1/1/2022 |
0.1.9 | 264 | 1/1/2022 |
0.1.8 | 279 | 1/1/2022 |
0.1.0 | 308 | 1/1/2022 |