gpt4 book ai didi

c# - FluentValidation - 值必须是可分配的;参数名称 : left

转载 作者:行者123 更新时间:2023-12-05 08:10:04 26 4
gpt4 key购买 nike

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

最佳答案

仔细检查一下,我想知道这是不是一个错误,也许有人可以指出这一点。

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

如您所见,我在值上添加了一个转换(短)。现在好像有用了??非常奇怪的行为。

关于c# - FluentValidation - 值必须是可分配的;参数名称 : left,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28541380/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com