February 2019
Intermediate to advanced
626 pages
15h 51m
English
The -Throw assertion is used to test whether a block of code throws a terminating error. Throw has a number of different usage scenarios. The simplest is detecting whether a terminating error (of any kind) is thrown at all:
function Invoke-Something { throw}Describe Invoke-Something {
It 'Throws a terminating error' {
{ Invoke-Something } | Should Throw
}
}
When testing for terminating errors, the subject of the test is placed in a script block (curly braces).
The next step might be to get Pester to test the error message, to ensure that the right error is thrown:
function Invoke-Something { throw 'an error'}Describe Invoke-Something { It 'Throws a terminating error' { { Invoke-Something } | Should Throw 'an error' } } ...Read now
Unlock full access