From cbb7b928df36d68b74e1674d01f96e65175dc54b Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Fri, 17 Jan 2025 11:20:25 +0100 Subject: [PATCH] Add type example in BeOfType help (#339) --- docs/assertions/assertions.mdx | 18 +++++++++++------- .../version-v5/assertions/assertions.mdx | 18 +++++++++++------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/docs/assertions/assertions.mdx b/docs/assertions/assertions.mdx index d645b7b..872beb3 100644 --- a/docs/assertions/assertions.mdx +++ b/docs/assertions/assertions.mdx @@ -163,20 +163,24 @@ $null | Should -Not -BeNullOrEmpty # Test will fail ### BeOfType -Asserts that the actual value should be an object of a specified type (or a subclass of the specified type) using PowerShell's -is operator: +Asserts that the actual value should be an object of a specified type (or a subclass of the specified type) using PowerShell's -is operator. +Expected type can be provided using full type name strings or a type wrapped in parantheses. ```powershell $actual = Get-Item $env:SystemRoot -$actual | Should -BeOfType System.IO.DirectoryInfo # Test will pass; object is a DirectoryInfo -$actual | Should -BeOfType System.IO.FileSystemInfo # Test will pass; DirectoryInfo base class is FileSystemInfo +$actual | Should -BeOfType System.IO.DirectoryInfo # Test will pass; object is a DirectoryInfo +$actual | Should -BeOfType ([System.IO.DirectoryInfo]) # Test will pass; object is a DirectoryInfo +$actual | Should -BeOfType System.IO.FileSystemInfo # Test will pass; DirectoryInfo base class is FileSystemInfo -$actual | Should -BeOfType System.IO.FileInfo # Test will fail; FileInfo is not a base class of DirectoryInfo +$actual | Should -BeOfType System.IO.FileInfo # Test will fail; FileInfo is not a base class of DirectoryInfo ``` -:::note - -This currently only works for .NET types. For custom type name, added using PSTypeNames, you need to use `$MyClass.GetType().Name | Should -Be "MyClassName"`. See [this issue](https://github.com/pester/Pester/issues/1315) for more information. +:::note Asserting PowerShell classes +PowerShell classes are not always visible to Pester due to PowerShell scoping behavior. As a workaround, use `$obj | Should -BeOfType ([MyClassName])` for exported classes or `$obj.GetType().Name | Should -Be "MyClassName"` for internal classes. See [this issue](https://github.com/pester/Pester/issues/2414). +::: +:::note Asserting PSTypeNames in custom objects +PSCustomObjects with custom `PSTypeName` are not recognized by the `-is` operator in PowerShell used by `Should -BeOfType`. As a workaround, use `$obj.PSTypeNames[0] | Should -Be 'SomeType'`. See [this issue](https://github.com/pester/Pester/issues/1315) for more information. ::: ### BeTrue diff --git a/versioned_docs/version-v5/assertions/assertions.mdx b/versioned_docs/version-v5/assertions/assertions.mdx index 2c67cb7..d4cf0ab 100644 --- a/versioned_docs/version-v5/assertions/assertions.mdx +++ b/versioned_docs/version-v5/assertions/assertions.mdx @@ -163,20 +163,24 @@ $null | Should -Not -BeNullOrEmpty # Test will fail ### BeOfType -Asserts that the actual value should be an object of a specified type (or a subclass of the specified type) using PowerShell's -is operator: +Asserts that the actual value should be an object of a specified type (or a subclass of the specified type) using PowerShell's -is operator. +Expected type can be provided using full type name strings or a type wrapped in parantheses. ```powershell $actual = Get-Item $env:SystemRoot -$actual | Should -BeOfType System.IO.DirectoryInfo # Test will pass; object is a DirectoryInfo -$actual | Should -BeOfType System.IO.FileSystemInfo # Test will pass; DirectoryInfo base class is FileSystemInfo +$actual | Should -BeOfType System.IO.DirectoryInfo # Test will pass; object is a DirectoryInfo +$actual | Should -BeOfType ([System.IO.DirectoryInfo]) # Test will pass; object is a DirectoryInfo +$actual | Should -BeOfType System.IO.FileSystemInfo # Test will pass; DirectoryInfo base class is FileSystemInfo -$actual | Should -BeOfType System.IO.FileInfo # Test will fail; FileInfo is not a base class of DirectoryInfo +$actual | Should -BeOfType System.IO.FileInfo # Test will fail; FileInfo is not a base class of DirectoryInfo ``` -:::note - -This currently only works for .NET types. For custom type name, added using PSTypeNames, you need to use `$MyClass.GetType().Name | Should -Be "MyClassName"`. See [this issue](https://github.com/pester/Pester/issues/1315) for more information. +:::note Asserting PowerShell classes +PowerShell classes are not always visible to Pester due to PowerShell scoping behavior. As a workaround, use `$obj | Should -BeOfType ([MyClassName])` for exported classes or `$obj.GetType().Name | Should -Be "MyClassName"` for internal classes. See [this issue](https://github.com/pester/Pester/issues/2414). +::: +:::note Asserting PSTypeNames in custom objects +PSCustomObjects with custom `PSTypeName` are not recognized by the `-is` operator in PowerShell used by `Should -BeOfType`. As a workaround, use `$obj.PSTypeNames[0] | Should -Be 'SomeType'`. See [this issue](https://github.com/pester/Pester/issues/1315) for more information. ::: ### BeTrue