0

Fluent Assertions is a .NET library for assertions of any kind, but I cannot get it to work with PowerShell. Is it possible at all?

I used Add-Type cmdlet to add the library's DLLs and tried the following example:

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

But got the following error:

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
YMM
  • 632
  • 1
  • 10
  • 21
  • 1
    Have you taken a look at Pester (https://github.com/pester/Pester)? It's written for Powershell testing and so doesn't have the verbosity of a native .NET library. – Richard Szalay Jul 11 '16 at 03:40
  • Pester is good, but does not compare to this library. And as far as I rememeber,it requires certain file structure and syntax to work, i.e. I cannot just use an assertion itself without Describe block etc. – YMM Jul 11 '16 at 09:51

2 Answers2

1

I think you're going to have to call it as an explicit static method.

In theory, this would be:

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

However, while this is a simple example, you may have to explitly type the arguments to avoid ambiguity later. Powershell will most likely implicitly make the arguments PSObject which gets resolved to Object instead of string or any other concrete type.

Eris
  • 7,378
  • 1
  • 30
  • 45
0

Should, as well as all of the assertions in Fluent assertions, are extension methods which are not supported by PowerShell.

I'd recommend using a PowerShell specific assertion library, or else a .net library with regular methods for syntax.

In addition to the extension methods, see this question for what's involved in in coming generic methods on non-generic classes.

Community
  • 1
  • 1
Richard Szalay
  • 83,269
  • 19
  • 178
  • 237