【问题标题】:Is it possible to use Fluent Assertions library with PowerShell?是否可以将 Fluent Assertions 库与 PowerShell 一起使用?
【发布时间】:2016-11-12 20:27:39
【问题描述】:

Fluent Assertions 是一个用于任何类型断言的 .NET 库,但我无法让它与 PowerShell 一起使用。有可能吗?

我使用 Add-Type cmdlet 添加库的 DLL 并尝试了以下示例:

PS C:\Users\ymm> $test = 'test string'
PS C:\Users\ymm> $test.[FluentAssertions.AssertionExtensions]::Should().BeNull()

但出现以下错误:

Cannot find an overload for "Should" and the argument count: "0".
At line:1 char:1
+ $test.[FluentAssertions.AssertionExtensions]::Should().BeNull()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest

【问题讨论】:

  • 你看过 Pester (github.com/pester/Pester) 吗?它是为 Powershell 测试而编写的,因此没有原生 .NET 库的冗长。
  • Pester 不错,但不能与这个库相比。据我记得,它需要一定的文件结构和语法才能工作,即我不能只使用没有描述块等的断言本身。

标签: powershell fluent-assertions


【解决方案1】:

我认为您将不得不将其称为显式静态方法。

理论上,这将是:

[FluentAssertions.AssertionExtensions]::Should($test).BeNull()

但是,虽然这是一个简单的示例,但您可能必须明确键入参数以避免以后出现歧义。 Powershell 很可能会隐式地将参数 PSObject 解析为 Object 而不是 string 或任何其他具体类型。

【讨论】:

    【解决方案2】:

    Should 以及 Fluent 断言中的所有断言都是 PowerShell 不支持的扩展方法。

    我建议使用特定于 PowerShell 的断言库,或者使用具有常规语法方法的 .net 库。

    除了扩展方法之外,请参阅this question,了解非泛型类上即将推出的泛型方法所涉及的内容。

    【讨论】:

      猜你喜欢
      • 2018-05-20
      • 2011-05-15
      • 2020-08-07
      • 2018-02-07
      • 2017-11-24
      • 2016-01-13
      • 2013-07-18
      • 2021-11-12
      • 2016-04-01
      相关资源
      最近更新 更多