Skip to content

Commit 356c9bc

Browse files
committed
test: 更新单元测试
1 parent fad547d commit 356c9bc

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

test/UnitTest/Components/ValidateFormTest.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,27 @@ public async Task Validate_Service_Ok()
566566
var msg = cut.FindComponent<MockInput<string>>().Instance.GetErrorMessage();
567567
Assert.Equal(HasServiceAttribute.Success, msg);
568568
}
569+
570+
[Fact]
571+
public async Task TestService_Ok()
572+
{
573+
// 自定义验证规则没有使用约定 Attribute 结尾单元测试
574+
var foo = new HasService();
575+
var cut = Context.RenderComponent<ValidateForm>(pb =>
576+
{
577+
pb.Add(a => a.Model, foo);
578+
pb.AddChildContent<MockInput<string>>(pb =>
579+
{
580+
pb.Add(a => a.Value, foo.Tag2);
581+
pb.Add(a => a.ValueExpression, Utility.GenerateValueExpression(foo, "Tag2", typeof(string)));
582+
pb.Add(a => a.ValidateRules, [new FormItemValidator(new TestValidateRule())]);
583+
});
584+
});
585+
var form = cut.Find("form");
586+
await cut.InvokeAsync(() => form.Submit());
587+
var msg = cut.FindComponent<MockInput<string>>().Instance.GetErrorMessage();
588+
Assert.Equal("Test", msg);
589+
}
569590

570591
[Fact]
571592
public async Task RequiredValidator_Ok()
@@ -726,7 +747,7 @@ public void ShowAllInvalidResult_Ok()
726747
private class HasServiceAttribute : ValidationAttribute
727748
{
728749
public const string Success = "Has Service";
729-
public const string Error = "No Service";
750+
private const string Error = "No Service";
730751

731752
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
732753
{
@@ -738,10 +759,21 @@ private class HasServiceAttribute : ValidationAttribute
738759
}
739760
}
740761

762+
private class TestValidateRule : ValidationAttribute
763+
{
764+
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
765+
{
766+
return new("Test");
767+
}
768+
}
769+
741770
private class HasService
742771
{
743772
[HasService]
744773
public string? Tag { get; set; }
774+
775+
[TestValidateRule]
776+
public string? Tag2 { get; set; }
745777
}
746778

747779
[MetadataType(typeof(DummyMetadata))]

0 commit comments

Comments
 (0)