简体   繁体   English

不确定如何使用 FluentValidation 测试此 .NET 字符串

[英]Not sure how to test this .NET string with FluentValidation

REF: A .NET Fiddle of (basically) the code, below. REF:(基本上)代码的.NET Fiddle ,如下。

I'm trying to test if a string is a valid Uri using FluentValidation:我正在尝试使用 FluentValidation 测试string是否是有效的Uri

public class LinksValidator : AbstractValidator<string>
{
    public LinksValidator()
    {
        RuleFor(x => x)
            .Must(LinkMustBeAUri)
            .WithMessage("Link '{PropertyValue}' must be a valid URI. eg: http://www.SomeWebSite.com.au");
    }

    private static bool LinkMustBeAUri(string link)
    {
        if (string.IsNullOrWhiteSpace(link))
        {
            return false;
        }

        Uri result;
        return Uri.TryCreate(link, UriKind.Absolute, out result);
    }
}

and for the validation test...和验证测试...

public class LinksValidatorTests
{
    private readonly LinksValidator _linksValidator;

    public LinksValidatorTests()
    {
        _linksValidator = new LinksValidator();    
    }

    [Theory]
    [InlineData("Http://www.SomeDomain.com")]
    [InlineData("https://www.SomeDomain.com")]
    [InlineData("http://www.SomeDomain.com.au")]
    public void GivenAValidUri_Validate_ShouldNotHaveAValidationError(string uri)
    {
        // Arrange.

        // Act & Assert.
        _linksValidator.ShouldNotHaveValidationErrorFor(l => l, uri);
    }
}

but the last line in that test method doesn't compile:但该测试方法中的最后一行无法编译:

在此处输入图片说明

How can I provide a hint to the compiler to tell it which method to use?如何向编译器提供提示以告诉它使用哪种方法?

So, it's not possible since it's within a library you can't modify.因此,这是不可能的,因为它位于您无法修改的库中。 See here .这里

There is a work-around, however.但是,有一个解决方法。 And that's to make sure that the generic arguments are not the same.这就是确保通用参数是一样的。

Here is working code:这是工作代码:

public static void Main()
{
    Console.WriteLine("Hello World");

    var uri = "Http://www.SomeDomain.com";
    var linksValidator = new LinksValidator();
    linksValidator.ShouldNotHaveValidationErrorFor(uri);
}

public class LinksValidator : AbstractValidator<LinksValidator.UrlWrapper>
{
    public class UrlWrapper { public string Url { get; set; } }
    public LinksValidator()
    {
        RuleFor(x => x.Url)
            .Must(LinkMustBeAUri)
            .WithMessage("Link '{PropertyValue}' must be a valid URI. eg: http://www.SomeWebSite.com.au");
    }

    //Optional 'overload' since l => l.Url seems to be superfluous
    public void ShouldNotHaveValidationErrorFor(string uri)
    {
        this.ShouldNotHaveValidationErrorFor(l => l.Url, uri);
    }

    private static bool LinkMustBeAUri(string link)
    {
        if (string.IsNullOrWhiteSpace(link))
        {
            return false;
        }

        //Courtesy of @Pure.Krome's comment and https://stackoverflow.com/a/25654227/563532
        Uri outUri;
        return Uri.TryCreate(link, UriKind.Absolute, out outUri)
               && (outUri.Scheme == Uri.UriSchemeHttp || outUri.Scheme == Uri.UriSchemeHttps);
    }
}

I'm not too familiar with how the FluentValidation library works - so there may be a tidier way to wrap this hack我不太熟悉 FluentValidation 库的工作原理 - 所以可能有一种更整洁的方式来包装这个 hack

Also note, I changed the way you detect Uri strings.另请注意,我更改了您检测Uri字符串的方式。 TryCreate will return true for just about any string (I was unable to find a case where it was ever false).对于几乎任何字符串, TryCreate都会返回 true(我无法找到它曾经为 false 的情况)。

I wrote it like this (so it's reusable):我是这样写的(所以它是可重用的):

public static class Validations
{
    public static IRuleBuilderOptions<T, string> Url<T>(this IRuleBuilder<T, string> ruleBuilder)
    {
        bool UrlIsValidUri(string url) => Uri.TryCreate(url, UriKind.Absolute, out var outUri)
           && (outUri.Scheme == Uri.UriSchemeHttp || outUri.Scheme == Uri.UriSchemeHttps);
        return ruleBuilder.Must(UrlIsValidUri);
    }
}

and use/test it like this (with xUnit):并像这样使用/测试它(使用 xUnit):

public class ValidationsTest
{
    [Theory]
    [InlineData("google.com", false)]
    [InlineData("https://google.com", true)]
    public void UrlValidationWorks(string url, bool expectedIsValid)
    {
        var entity = new TestEntity
        {
            Url = url
        };
        var validator = new TestEntityValidator();
        var result = validator.Validate(entity);
        Assert.Equal(expectedIsValid, result.IsValid);
    }

    private class TestEntity
    {
        public string Url { get; set; }
    }

    private class TestEntityValidator : AbstractValidator<TestEntity>
    {
        public TestEntityValidator()
        {
            RuleFor(x => x.Url).Url();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM