FluentValidation-值必须可赋值;参数名称:left



我正试图在单元测试中模拟我的fluentValidation验证器。出于某种原因,我有一对夫妇抛出了这个错误?

这是我的实体(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;
    }
}

这是我的验证器

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");
    }
}

这是我的模拟测试(失败)

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);
}

出于某种原因,这些测试抛出了一个错误。这是其中一个测试的错误,其余的都是一样的,只是的方法不同

Test Name:  ContentValidator_ShouldHaveErrorWhenPositionGreaterThanTwoHundredAndFiftyFive
Test FullName:  Tests.TestContentValidator.ContentValidator_ShouldHaveErrorWhenPositionGreaterThanTwoHundredAndFiftyFive
Test Source:    e:Sample ProjectsTestsTestContentValidator.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:ProjectsFluentValidationsrcFluentValidationMemberAccessor.cs:line 29
   at FluentValidation.MemberAccessor`2..ctor(Expression`1 getExpression) in c:ProjectsFluentValidationsrcFluentValidationMemberAccessor.cs:line 23
   at FluentValidation.MemberAccessor`2.op_Implicit(Expression`1 this) in c:ProjectsFluentValidationsrcFluentValidationMemberAccessor.cs:line 66
   at FluentValidation.TestHelper.ValidatorTester`2..ctor(Expression`1 expression, IValidator`1 validator, TValue value, String ruleSet) in c:ProjectsFluentValidationsrcFluentValidationTestHelperValidatorTester.cs:line 33
   at FluentValidation.TestHelper.ValidationTestExtension.ShouldHaveValidationErrorFor[T,TValue](IValidator`1 validator, Expression`1 expression, TValue value, String ruleSet) in c:ProjectsFluentValidationsrcFluentValidationTestHelperValidatorTestExtensions.cs:line 29
   at Tests.TestContentValidator.ContentValidator_ShouldHaveErrorWhenPositionGreaterThanTwoHundredAndFiftyFive() in e:Sample ProjectsTestsTestContentValidator.cs:line 76

仔细研究一下,我想知道这是否是一个bug,也许有人可以指出这一点。

如果我更改代码并将值强制转换为short,它似乎有效吗?

所以它现在变成了这个

[TestMethod]
public void ContentValidator_ShouldHaveErrorWhenPositionLessThanOne()
{
    _validator.ShouldHaveValidationErrorFor(e => e.Position, (short)0);
}
[TestMethod]
public void ContentValidator_ShouldHaveErrorWhenPositionGreaterThanTwoHundredAndFiftyFive()
{
    _validator.ShouldHaveValidationErrorFor(e => e.Position, (short)256);
}
[TestMethod]
public void ContentValidator_ShouldHaveErrorWhenWidthLessThanOne()
{
    _validator.ShouldHaveValidationErrorFor(e => e.Width, (short)0);
}
[TestMethod]
public void ContentValidator_ShouldHaveErrorWhenWidthGreaterThanTwelve()
{
    _validator.ShouldHaveValidationErrorFor(e => e.Width, (short)13);
}

正如你所看到的,我在这些值上添加了一个cast(short)。现在它似乎起作用了??非常奇怪的行为。

相关内容

最新更新