Advertisement
Guest User

NullReference exception throw by FluentValidation when mocki

a guest
Sep 5th, 2019
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.89 KB | None | 0 0
  1.   using FluentAssertions;
  2.   using FluentValidation;
  3.   using FluentValidation.Results;
  4.   using NSubstitute;
  5.   using System;
  6.   using System.Collections.Generic;
  7.  
  8.   namespace SubValidationTest
  9.   {
  10.   class Program
  11.   {
  12.     static void Main()
  13.     {
  14.       Test_ChildValidator();
  15.       Test_ParentValidator_WithRealChildValidator();
  16.       Test_ParentValidator_WithMockedChildValidator();
  17.     }
  18.  
  19.     static void Test_ChildValidator()
  20.     {
  21.       var child = Substitute.For<IChild>();
  22.       var validator = new ChildValidator();
  23.  
  24.       child.Value.Returns(null as string);
  25.       validator.Validate(child).IsValid.Should().BeFalse();
  26.  
  27.       child.Value.Returns("");
  28.       validator.Validate(child).IsValid.Should().BeFalse();
  29.  
  30.       child.Value.Returns("a");
  31.       validator.Validate(child).IsValid.Should().BeTrue();
  32.     }
  33.  
  34.     static void Test_ParentValidator_WithRealChildValidator()
  35.     {
  36.       var child = Substitute.For<IChild>();
  37.       var childValidator = new ChildValidator();
  38.  
  39.       var parent = Substitute.For<IParent>();
  40.       var validator = new ParentValidator(childValidator);
  41.  
  42.       parent.Child.Returns(null as IChild);
  43.       validator.Validate(parent).IsValid.Should().BeTrue();
  44.  
  45.       parent.Child.Returns(child);
  46.       validator.Validate(parent).IsValid.Should().BeFalse();
  47.  
  48.       child.Value.Returns("a");
  49.       validator.Validate(parent).IsValid.Should().BeTrue();
  50.     }
  51.  
  52.     static void Test_ParentValidator_WithMockedChildValidator()
  53.     {
  54.       var child = Substitute.For<IChild>();
  55.       var childValidator = Substitute.For<IValidator<IChild>>();
  56.  
  57.       var parent = Substitute.For<IParent>();
  58.       var validator = new ParentValidator(childValidator);
  59.  
  60.       parent.Child.Returns(null as IChild);
  61.       validator.Validate(parent).IsValid.Should().BeTrue();
  62.  
  63.       parent.Child.Returns(child);
  64.      
  65.       childValidator.Validate(Arg.Any<IChild>()).Returns(new ValidationResult(new List<ValidationFailure> {  new ValidationFailure("property", "message") }));
  66.       validator.Validate(parent).IsValid.Should().BeFalse();
  67.  
  68.       childValidator.Validate(Arg.Any<IChild>()).Returns(new ValidationResult());
  69.       validator.Validate(parent).IsValid.Should().BeTrue();
  70.     }
  71.   }
  72.  
  73.     public interface IChild
  74.     {
  75.       string Value { get; }
  76.     }
  77.  
  78.     public class ChildValidator : AbstractValidator<IChild>
  79.     {
  80.       public ChildValidator()
  81.       {
  82.         RuleFor(c => c.Value)
  83.           .NotEmpty()
  84.           .NotEmpty()
  85.           .WithMessage("Friendly Error Message");
  86.       }
  87.     }
  88.  
  89.     public interface IParent
  90.     {
  91.       IChild Child { get; }
  92.     }
  93.  
  94.     public class ParentValidator : AbstractValidator<IParent>
  95.     {
  96.       public ParentValidator(IValidator<IChild> childValidator)
  97.       {
  98.         When(p => p.Child != null, () => {
  99.           RuleFor(p => p.Child)
  100.             .SetValidator(childValidator);
  101.         });
  102.       }
  103.     }
  104.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement