SparkyTestHelpers.DataAnnotations 1.1.1

Install-Package SparkyTestHelpers.DataAnnotations -Version 1.1.1
dotnet add package SparkyTestHelpers.DataAnnotations --version 1.1.1
<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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: SparkyTestHelpers.DataAnnotations, 1.1.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. 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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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:


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):

    .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:

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

// Multi-member validation result:
    .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:

    .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.
    .WithValidationContextItem("key", "value")

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 372 7/27/2019
1.1.0 486 12/8/2018
1.0.0 658 3/19/2018

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