【问题标题】:Fluent Assertions compare string to Guid流利的断言将字符串与 Guid 进行比较
【发布时间】:2014-02-13 23:09:24
【问题描述】:

我正在尝试找到最流畅的方式来断言某个字符串是有效的 Guid。

iterTags.GUIDstring

我的第一次尝试以错误告终,因为string没有实现Guid。好的,我看到它来了,因为它是在黑暗中拍摄的

iterTags.GUID.Should().BeAssignableTo<Guid>();

所以我想出了这个可行的解决方案,但它并不流畅

Guid parsedGuid;
if (!Guid.TryParseExact(iterTags.GUID, "D", out parsedGuid))
    Assert.Fail("iterTags.GUID: '{0}' is not a valid guid");

阅读documentation,我发现没有更好的方法来进行断言。

我的问题:是否有一种流畅的方式来断言一个字符串是一个有效的Guid

也许,类似...

iterTags.GUID.Should().BeParsedAs<Guid>()

【问题讨论】:

    标签: c# fluent-assertions


    【解决方案1】:
    Guid parsedGuid;
    Guid.TryParseExact(iterTags.GUID, "D", out parsedGuid).Should.BeTrue("because {0} is a valid Guid string representation", iterTags.GUID);
    

    new Action(() => new Guid(iterTags.GUID)).ShouldNotThrow("because {0} is a valid Guid string representation", iterTags.GUID);
    

    【讨论】:

    • 很好的解决方案。我只需要显式创建一个动作: new Action(() => new Guid(iterTags.GUID)).ShouldNotThrow("");
    • 您是否愿意纠正您的第二个解决方案(或者如果它在您的 VS 中编译,请告诉我我错了)
    • 当然。我没有安装 Fluent Assertions,只是感兴趣并阅读了文档:)
    • 另一种选择可能是编写一个扩展 - 但这肯定需要引用 Fluent Assertions 来测试代码。
    • 我确实引用了它,所以这不会是问题。我应该看到文档的正确部分,但老实说,我没有理解它 - 为更好的人铺平道路:) 谢谢
    【解决方案2】:

    您还可以对 GUID 使用 not be empty 检查,这样您就可以使用 FluentAssertions 的 空检查:

    Guid.TryParse(iterTags.GUID, out var parsedIterTagsGUID)
    parsedIterTagsGUID.Should().NotBeEmpty();
    

    或作为扩展:

        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);
        }
    

    上面可以做得更好,通过扩展其他类似的东西:

        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);
        }
    

    或者通过在https://github.com/fluentassertions/fluentassertions 上提出拉取请求可能会更好

    【讨论】:

      【解决方案3】:

      Nick N 的回答启发,这就是我最终的结果。

      主要区别在于失败消息更清楚地表明字符串不是 GUID,NotBeEmpty 条件将返回一条消息,指出输入为空。

      public static class CustomAssertions
      {
          public static AndConstraint<StringAssertions> BeGuid(this StringAssertions assertions, string because = "", params object[] becauseArgs)
          {
              var isGuid = Guid.TryParse(assertions.Subject, out _);
      
              Execute.Assertion
                 .ForCondition(isGuid)
                 .BecauseOf(because, becauseArgs)
                 .FailWith("Expected a GUID converted to a string{reason}, but found {0}.", assertions.Subject);
      
              return new AndConstraint<StringAssertions>(assertions);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-02-25
        • 2017-09-20
        • 2019-12-14
        • 2012-10-06
        • 2019-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多