lundi 16 février 2015

FluentValidation - Value must be assignable; Parameter name: left

I am trying to mock my fluentValidation validators in unit tests. For some reason i have a couple that throw this error?


Here is my entity (EF6)



public class Content : AuditableEntity
{
public long PageId { get; set; }

public virtual Page Page { get; set; }

public short Position { get; set; }

public short Width { get; set; }

public Content()
{
Position = 1;
}

}


Here is my validator



public class ContentValidator : AbstractValidator<Content>
{
public ContentValidator()
{
RuleFor(e => e.Page).NotNull().WithMessage("You must select a page for this content").When(e => e.PageId == 0);

RuleFor(e => e.Position).GreaterThan((short)0).LessThan((short)256).WithMessage("Position must be between 1 and 255");

RuleFor(e => e.Width).GreaterThan((short)0).LessThan((short)13).WithMessage("Width must be between 1 and 12");
}
}


Here are my mock tests (that fail)



private ContentValidator _validator;

[TestInitialize]
public void TestInit()
{
_validator = new ContentValidator();
}
[TestMethod]
public void ContentValidator_ShouldHaveErrorWhenPositionLessThanOne()
{
_validator.ShouldHaveValidationErrorFor(e => e.Position, 0);
}

[TestMethod]
public void ContentValidator_ShouldHaveErrorWhenPositionGreaterThanTwoHundredAndFiftyFive()
{
_validator.ShouldHaveValidationErrorFor(e => e.Position, 256);
}

[TestMethod]
public void ContentValidator_ShouldHaveErrorWhenWidthLessThanOne()
{
_validator.ShouldHaveValidationErrorFor(e => e.Width, 0);
}

[TestMethod]
public void ContentValidator_ShouldHaveErrorWhenWidthGreaterThanTwelve()
{
_validator.ShouldHaveValidationErrorFor(e => e.Width, 13);
}


For some reason these tests throw an error. Here is the error from one of the tests, the rest are all the same, just different method



Test Name: ContentValidator_ShouldHaveErrorWhenPositionGreaterThanTwoHundredAndFiftyFive
Test FullName: Tests.TestContentValidator.ContentValidator_ShouldHaveErrorWhenPositionGreaterThanTwoHundredAndFiftyFive
Test Source: e:\Sample Projects\Tests\TestContentValidator.cs : line 75
Test Outcome: Failed
Test Duration: 0:00:00.001921

Result Message:
Test method TestContentValidator.ContentValidator_ShouldHaveErrorWhenPositionGreaterThanTwoHundredAndFiftyFive threw exception:
System.ArgumentException: Expression must be writeable
Parameter name: left
Result StackTrace:
at System.Linq.Expressions.Expression.RequiresCanWrite(Expression expression, String paramName)
at System.Linq.Expressions.Expression.Assign(Expression left, Expression right)
at FluentValidation.MemberAccessor`2.CreateSetExpression(Expression`1 getExpression) in c:\Projects\FluentValidation\src\FluentValidation\MemberAccessor.cs:line 29
at FluentValidation.MemberAccessor`2..ctor(Expression`1 getExpression) in c:\Projects\FluentValidation\src\FluentValidation\MemberAccessor.cs:line 23
at FluentValidation.MemberAccessor`2.op_Implicit(Expression`1 this) in c:\Projects\FluentValidation\src\FluentValidation\MemberAccessor.cs:line 66
at FluentValidation.TestHelper.ValidatorTester`2..ctor(Expression`1 expression, IValidator`1 validator, TValue value, String ruleSet) in c:\Projects\FluentValidation\src\FluentValidation\TestHelper\ValidatorTester.cs:line 33
at FluentValidation.TestHelper.ValidationTestExtension.ShouldHaveValidationErrorFor[T,TValue](IValidator`1 validator, Expression`1 expression, TValue value, String ruleSet) in c:\Projects\FluentValidation\src\FluentValidation\TestHelper\ValidatorTestExtensions.cs:line 29
at Tests.TestContentValidator.ContentValidator_ShouldHaveErrorWhenPositionGreaterThanTwoHundredAndFiftyFive() in e:\Sample Projects\Tests\TestContentValidator.cs:line 76

Aucun commentaire:

Enregistrer un commentaire