FluentValidation - 值必须可分配;参数名称:左

时间:2015-02-16 12:23:32

标签: c# .net validation unit-testing fluentvalidation

我试图在单元测试中模拟我的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 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

1 个答案:

答案 0 :(得分:-1)

转过头来,我想知道这是不是一个错误,也许有人可以指出这一点。

如果我更改代码并将值转换为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);
}

正如您所看到的,我在值上添加了一个演员(简称)。现在它似乎工作?非常奇怪的行为。