Skip to main content
Version: v6

Should-HaveType

This page was generated

Contributions are welcome in Pester-repo.

SYNOPSIS

Asserts that the input is of the expected type.

SYNTAX

Should-HaveType [-Expected] <Object> [[-Actual] <Object>] [-Because <string>]

DESCRIPTION

This assertion uses -is to verify that the actual value is assignable to the expected type. Derived types and implemented interfaces also satisfy the check.

When the expected type is given as a name that does not resolve to a loaded .NET type, the assertion falls back to matching against the actual value's PSTypeNames. This allows asserting against PowerShell custom types, such as objects tagged with a PSTypeName or extended with Add-Member -TypeName.

EXAMPLES

EXAMPLE 1

"hello" | Should-HaveType ([String])
1 | Should-HaveType ([Int32])

These assertions will pass, because the actual value is of the expected type.

EXAMPLE 2

$actual = [PSCustomObject]@{ PSTypeName = 'MyApp.Person'; Name = 'Jane' }
$actual | Should-HaveType 'MyApp.Person'

This assertion will pass, because 'MyApp.Person' is not a loaded .NET type, so the actual value's PSTypeNames are checked instead.

PARAMETERS

-Actual

The actual value.

Type: System.Object
DefaultValue: ''
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
Position: 1
IsRequired: false
ValueFromPipeline: true
ValueFromPipelineByPropertyName: false
ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-Because

The reason why the input should be the expected type.

Type: System.String
DefaultValue: ''
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
Position: Named
IsRequired: false
ValueFromPipeline: false
ValueFromPipelineByPropertyName: false
ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-Expected

The expected type. Provide a [Type], a type name that resolves to a loaded .NET type, or a custom type name to match against the actual value's PSTypeNames.

Type: System.Object
DefaultValue: ''
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
Position: 0
IsRequired: true
ValueFromPipeline: false
ValueFromPipelineByPropertyName: false
ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTS

System.Object

OUTPUTS

NOTES

Use the -ErrorAction parameter to control soft-assertion behavior for this assertion. -ErrorAction Continue records the failure and lets the rest of the test run (a soft assertion), while -ErrorAction Stop fails the test immediately, for example to guard a precondition before continuing.

When -ErrorAction is not specified, the behavior comes from Should.ErrorAction in the configuration, which defaults to Stop. See https://pester.dev/docs/assertions/soft-assertions for more about soft assertions.

VERSION

This page was generated using comment-based help in Pester 6.1.0.