Is there a way to assert if an incomming string is a Guid using Fluent Assertions?
Something like
var guid = "f0c70613-2667-4a9a-8c2c-69006e100d790";
guid.Should().BeOfType<Guid>();
It would involve casting the input string to a Guid type and that would be ok.
Currently using Guid.Parse(guid);
and relying on the exception on incorrect input, but it's not very fluent-like.
Using Guid.TryParse in a Fluent Style:
var guid = "f0c70613-2667-4a9a-8c2c-69006e100d790";
guid.Should().Match(g => Guid.TryParse(g, out _));
Using Guid.Parse with Exception Assertion:
var guid = "f0c70613-2667-4a9a-8c2c-69006e100d790";
Action act = () => Guid.Parse(guid);
act.Should().NotThrow<FormatException>();
Custom Assertion Method for String to Guid:
public static class GuidAssertions
{
public static void ShouldBeGuid(this string value)
{
value.Should().Match(g => Guid.TryParse(g, out _), "because the string should be a valid GUID.");
}
}
Then you can use it like this:
var guid = "f0c70613-2667-4a9a-8c2c-69006e100d790";
guid.ShouldBeGuid();