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>()
If you don't need to use the parsed Guid object further (requires C# 7 or above):
Guid.TryParseExact(iterTags.GUID, "D", out var _).Should().BeTrue("because {0} is a valid Guid string representation", iterTags.GUID);
or (does not require C# 7 or above)
new Action(() => new Guid(iterTags.GUID)).ShouldNotThrow("because {0} is a valid Guid string representation", iterTags.GUID);
If you need to use the parsed Guid for other things in your test, then:
Guid parsedGuid;
Guid.TryParseExact(iterTags.GUID, "D", out parsedGuid).Should().BeTrue("because {0} is a valid Guid string representation", iterTags.GUID);