SparkyTestHelpers.DataAnnotations 1.1.1

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

// Install SparkyTestHelpers.DataAnnotations as a Cake Tool
#tool nuget:?package=SparkyTestHelpers.DataAnnotations&version=1.1.1                

see also:


This NuGet package contains helper classes to enable unit testing of .NET models and entities that use System.ComponentModel.DataAnnotations ValidationAttributes via a fluent syntax.

Validation is initialized via the static Validation.For(obj) method:

using SparkyTestHelpers.DataAnnotation;
. . .
    var foo = new Foo { /* populated with valid values */ };
    ValidationForModel<Foo> validation = Validation.For(foo);

The .ValidationResults() method validates the object and returns the results:

IEnumerable<ValidationResult> validationResults = Validation.For(foo).ValidationResults();

...but you'll usually check validation results via other fluently-written methods:

Validation.For(foo).ShouldReturn.NoErrors();

The When(expression) method arranges entity adjustments to be tested. (It makes a "clone" of the instance passed to Validation.For() before making adjustments, so each "When" call starts fresh with the original values):

Validation.For(foo)
    .When(x => x.Bar = null)
    .ShouldReturn.RequiredErrorFor(x => x.Bar);

A code example above shows a call to .ShouldReturn.NoErrors() to assert that the model has no validation errors. .ShouldReturn.ErrorFor() is used to assert that a specific validation error was returned:

Validation.For(foo)
    .When(x => x.Bar = "13")
    .ShouldReturn.ErrorFor(x => x.Bar).WithMessage("Invalid Bar value. 13 is unlucky!");

// Multi-member validation result:
Validation.For(foo)
    .When(x => 
    { 
        x.Bar = "dog";
        x.Baz = "cat";
    })
    .ShouldReturn.ErrorFor(x => x.Bar).AndFor(x => x.Baz)
        .WithMessage("Invalid Bar/Baz combination.");

ShouldReturn.ErrrorFor(expression).ForValidationAttribute(<TAttribute>) is used to assert that a ValidationResult was created by a specific ValidationAttribute:

Validation.For(foo)
    .When(x => x.Bar = null)
    .ShouldReturn.ErrorFor(x => x.Bar).ForValidationAttribute<RequiredAttribute>();

There are also ShouldReturn. methods for standard ValidationAttributes:

  • [CreditCard]: ShouldReturn.CreditCardErrorFor(x ⇒ x.Foo);
  • [EmailAddress]: ShouldReturn.EmailAddressErrorFor(x ⇒ x.Foo);
  • [EnumDataType]: ShouldReturn.EnumDataTypeErrorFor(x ⇒ x.Foo);
  • [MaxLength]: ShouldReturn.MaxLengthErrorFor(x ⇒ x.Foo);
  • [MinLength]: ShouldReturn.MinLengthErrorFor(x ⇒ x.Foo);
  • [Phone]: ShouldReturn.PhoneErrorFor(x ⇒ x.Foo);
  • [Range]: ShouldReturn.RangeErrorFor(x ⇒ x.Foo);
  • [RegularExpression]: ShouldReturn.RegularExpressionErrorFor(x ⇒ x.Foo);
  • [Required]: ShouldReturn.RequiredErrorFor(x ⇒ x.Foo);
  • [StringLength]: ShouldReturn.StringLengthErrorFor(x ⇒ x.Foo);

You don't have to specify the expected error message for .ForValidationAttribute method or for the methods listed above. The validation tester automatically determines the error message from the attribute.

WithValidationContext... methods

  • WithValidationContextItems(IDictionary<object, object> items) - sets ValidationContext.Items available to the IValidatableObject.Validate method.
  • WithValidationContextItem(object key, object value) - Adds ValidationContextItems entry
  • WithValidationContextServiceProvider(IServiceProvider serviceProvider) - sets ValidationContext service provider available to the IValidatableObject.Validate method.
Validation.For(foo)
    .WithValidationContextItem("key", "value")
    .ShouldReturn...
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 is compatible.  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
1.1.1 725 7/27/2019
1.1.0 739 12/8/2018
1.0.0 988 3/19/2018

V1.1: Added "WithValidationContext..." methods