REF: A .NET Fiddle of (basically) the code, below.
I'm trying to test if a string
is a valid Uri
using FluentValidation:
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?