Skip to content

Should Throw - Needs more flexibility/power #279

@ghostsquad

Description

@ghostsquad

Asserting on the expected message in the exception is nice:

{ throw 'foo' } | Should Throw 'foo'

But what if I wanted to examine additional properties of the exception, or even the exception type? I'm forced to do this:

$caughtException = $null
try {
    throw (new-object system.notimplementedexception)
} catch {
    $caughtException = $_
}

$caughtException | Should Not Be $null
$caughtException.Exception.GetType() | Should Be ([System.NotImplementedException])
$caughtException.Exception.Message | Should ...
$caughtException.Exception.CustomProperty | Should ...

Proposed Syntax

$action = { throw (new-object system.notimplementedexception) }
$actualException = $action | Should Throw ([System.NotImplementedException]) "My Message" -PassThru
$actualException.CustomProperty | Should ...

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions