I am trying to find the most fluent manner to assert that a certain string is a valid Guid.
iterTags.GUID
is a string
.
My first attempt ended in error because string
does not implement Guid
. Okay, I saw it coming as it was a shot in the dark
iterTags.GUID.Should().BeAssignableTo<Guid>();
So i came up with this working solution but it's not fluent
Guid parsedGuid;
if (!Guid.TryParseExact(iterTags.GUID, "D", out parsedGuid))
Assert.Fail("iterTags.GUID: '{0}' is not a valid guid");
Reading the documentation I found no better way of doing the assertion.
My question: Is there a fluent way of asserting a string is a valid Guid
Perhaps, something like...
iterTags.GUID.Should().BeParsedAs<Guid>()
Guid parsedGuid;
Guid.TryParseExact(iterTags.GUID, "D", out parsedGuid).Should.BeTrue("because {0} is a valid Guid string representation", iterTags.GUID);
or
new Action(() => new Guid(iterTags.GUID)).ShouldNotThrow("because {0} is a valid Guid string representation", iterTags.GUID);
You could also use the not be empty
check on a GUID, that way you can use FluentAssertions's empty checking:
Guid.TryParse(iterTags.GUID, out var parsedIterTagsGUID)
parsedIterTagsGUID.Should().NotBeEmpty();
Or as extension:
public static AndConstraint<FluentAssertions.Primitives.GuidAssertions> ShouldBeGuid(this object value, string because = "", params object[] becauseArgs)
{
Guid.TryParse(value?.ToString(), out var guid);
return guid.Should().NotBeEmpty(because, becauseArgs);
}
The above can be done better, by extending something else like:
public static AndConstraint<GuidAssertions> BeGuid(this StringAssertions value, string because = "", params object[] becauseArgs)
{
Guid.TryParse(value.Subject, out var guid);
return guid.Should().NotBeEmpty(because, becauseArgs);
}
public static AndConstraint<GuidAssertions> BeGuid(this ObjectAssertions value, string because = "", params object[] becauseArgs)
{
return (value.Subject as Guid?).Should().NotBeNull().And.NotBeEmpty(because, becauseArgs);
}
Or maybe even better by proposing a pull request on: https://github.com/fluentassertions/fluentassertions
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.