Testing for terminating/non-terminating errors

67 views
Skip to first unread message

Trond Hindenes

unread,
Dec 8, 2015, 8:07:59 PM12/8/15
to Pester
Hi, 
Just getting started with Pester so bear with me :-)

I'd like to use Pester to test that a function doesn't throw (terminating) errors when it shouldn't.
As far as I can see the "should" keyword already has support for "throw", but in order to test for non-terminating errors I guess I have to look at the error stream and check that the expected error is output. I can't find any documentation on how to do that, is there a pattern I could implement in order to do this easily?

Dave Wyatt

unread,
Dec 8, 2015, 8:22:06 PM12/8/15
to Trond Hindenes, Pester
Well, if you want to know that it didn't throw a terminating error, just use "Should Not Throw".  If you want to do validation on the non-terminating error, then yeah, you'd need to capture the ErrorRecord yourself and do some tests on its properties.  Something like this:

It 'Produces an error' {
    Do-Something -ErrorAction SilentlyContinue -ErrorVariable myErr
    $myErr.Count | Should Be 1
    $myErr.Exception.Message | Should Be 'Holy crap, something went wrong!'
    # etc
}

--
You received this message because you are subscribed to the Google Groups "Pester" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pester+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Trond Hindenes

unread,
Dec 8, 2015, 8:29:29 PM12/8/15
to Pester, tr...@hindenes.com
Cool, that make sense. Thanks!
Reply all
Reply to author
Forward
0 new messages