NoWoL.TestingUtilities 0.1.32

dotnet add package NoWoL.TestingUtilities --version 0.1.32
NuGet\Install-Package NoWoL.TestingUtilities -Version 0.1.32
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="NoWoL.TestingUtilities" Version="0.1.32" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NoWoL.TestingUtilities --version 0.1.32
#r "nuget: NoWoL.TestingUtilities, 0.1.32"
#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 NoWoL.TestingUtilities as a Cake Addin
#addin nuget:?package=NoWoL.TestingUtilities&version=0.1.32

// Install NoWoL.TestingUtilities as a Cake Tool
#tool nuget:?package=NoWoL.TestingUtilities&version=0.1.32

NoWoL.TestingUtilities

Build Status Version Quality Gate Status Sonar Coverage Sonar Tests

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 parameters of a method or constructor you need to acquire a ParametersValidator. The library provides a helper class called ParametersValidatorHelper 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 = ParametersValidatorHelper.GetConstructorParametersValidator<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 = ParametersValidatorHelper.GetMethodParametersValidator(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.

Expressions

You can use a LINQ expression to avoid using magic strings for the name of the parameters. The helper below will automatically fill in the parameters' names in the order they are used in the expression. Using validator.For is currently required to correctly wire up the validator since ConstantExpression are not yet supported.

var obj = new TestClass();
var validator = ParametersValidatorHelper.GetExpressionParametersValidator(obj);
validator.Setup(x => x.MyMethod(validator.For<string>(ExpectedExceptionRules.None),
                                validator.For<List<int>>(ExpectedExceptionRules.NotNull, ExpectedExceptionRules.NotEmpty)))
         .Validate();

Automatic Setup

Configuring every parameters manually can be quite a chore. You can use the Setup method to automatically configure the parameters' rules of a method using predefined rules.

var obj = new TestClass();
var validator = ParametersValidatorHelper.GetMethodParametersValidator(obj, nameof(TestClass.MyMethod));
validator.SetupAll(ParametersValidator.DefaultRules) // The default rules are the predefined validation rules for the most common validations. You can define you own rules if the defaults do not work for you.
         .UpdateParameter("param1", ExpectedExceptionRules.NotValue("Freddie")) // You can call SetupAll to configure every parameters with the default rules and then use UpdateParameter to update any parameters which the default rules are not applicable
         .Validate();

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), objectCreators)); // 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(ParametersValidatorHelper.DefaultCreators); // add the default creators
var validator = ParametersValidatorHelper.GetMethodParametersValidator(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 = ParametersValidatorHelper.GetMethodParametersValidator(obj, 
                                                                       nameof(AnotherClass.AnotherMethod), 
                                                                       methodParameters: values);
...

Overriding default values

Specifying the method parameters when building the validator is great when most of the parameters require custom creation. It can be heavy to use if you only have one value that requires custom creation. You can use SetParameterValue method to help with this scenario: you can create your validator without specifying methodParameters and use SetParameterValue to override the default value for one or many parameters.

Note: You cannot mix methodParameters and SetParameterValue.

var obj = new TestClass();
var validator = ParametersValidatorHelper.GetMethodParametersValidator(obj, nameof(TestClass.MyMethod));
validator.SetupAll(ParametersValidator.DefaultRules) // Use the default rules or define your own
         .SetParameterValue("theParameterName", new ComplexType(1,2,3)) // Change the default value when validating
         .Validate();

Default Validation Rules

The library provides a default list of rules with the common validation (ParametersValidator.DefaultRules). The validation rules are configured like so:

  • String: ExpectedExceptionRules.NotNull, ExpectedExceptionRules.NotEmptyOrWhiteSpace
  • ValueTypes: ExpectedExceptionRules.SkipParameter
  • Interfaces: ExpectedExceptionRules.NotNull
  • CollectionTypes: ExpectedExceptionRules.NotNull
  • OtherTypes: ExpectedExceptionRules.NotNull

As you can see, the value types are configured to use the ExpectedExceptionRules.SkipParameter rules and will not be validate by default. You can change this behavior by creating your own default rules when creating the validator.

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 interface 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

MIT

Product 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. 
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
0.1.32 1,817 5/11/2022
0.1.31 399 5/7/2022
0.1.30 801 4/10/2022
0.1.29 413 4/10/2022
0.1.28 423 4/10/2022
0.1.27 437 2/1/2022
0.1.26 256 1/6/2022
0.1.25 255 1/6/2022
0.1.24 258 1/6/2022
0.1.23 257 1/4/2022
0.1.22 249 1/4/2022
0.1.21 266 1/4/2022
0.1.20 244 1/3/2022
0.1.19 263 1/3/2022
0.1.18 276 1/2/2022
0.1.17 246 1/2/2022
0.1.16 424 1/2/2022
0.1.15 267 1/2/2022
0.1.14 256 1/1/2022
0.1.12 263 1/1/2022
0.1.9 241 1/1/2022
0.1.8 268 1/1/2022
0.1.0 283 1/1/2022