Skip to content

Commit

Permalink
Fix error messsage of Should Throw when null input is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
nohwnd committed Apr 24, 2016
1 parent d9bd6d5 commit 138bf62
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Functions/Assertions/PesterThrow.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ InModuleScope Pester {
It "returns true if the statement throws an exception and the actual error text matches the expected error pattern" {
Test-PositiveAssertion (PesterThrow { throw "expected error"} "error")
}

It "throws ArgumentException if null ScriptBlock is provided" {
try {
Test-PositiveAssertion (PesterThrow $null)
throw "Should throw exception, but no exception was thrown."
}
catch [ArgumentException] {
#do nothing. we expect argument exception to happen
}
}
}

Describe "Get-DoMessagesMatch" {
Expand Down
5 changes: 5 additions & 0 deletions Functions/Assertions/PesterThrow.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ $ActualExceptionWasThrown = $false
# because this is a script block, the user will have to
# wrap the code they want to assert on in { }
function PesterThrow([scriptblock] $script, $expectedErrorMessage) {

if ($null -eq $script) {
throw (New-Object -TypeName ArgumentNullException -ArgumentList "script","Scriptblock not found. Input to 'Throw' and 'Not Throw' must be enclosed in curly braces.")
}

$Script:ActualExceptionMessage = ""
$Script:ActualExceptionWasThrown = $false

Expand Down

0 comments on commit 138bf62

Please sign in to comment.