Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update help for BeOfType #339

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions docs/assertions/assertions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 11 additions & 7 deletions versioned_docs/version-v5/assertions/assertions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down