Hi,
I am trying to write a pester test for when my function throws an exception and I am unable to get this to work. I modified this some from my original code but the idea is the same. When I run Pester it fails at this assertion with an exception and says the test failed.
I expect and receive the following exception.
Exception Details: "MethodInvocationException: Exception calling “Substring” with 2 arguments: Index and length must refer to a location within the string.
Please advise.
Thanks.
function Select-SubString { [CmdletBinding()] param( [Parameter(Mandatory = $true, ValueFromPipeline = $true)] [string]$input ) Write-Verbose "Taking input and creating substring variable" $substring = $($input.Substring(0, 25)) return $substring }
Import-Module MyModule Describe "myModule.psm1" { InModuleScope myModule { $string = "abc" it 'should throw exception' { { $string.toString() | Select-SubString } | Should Throw MethodInvocationException } } }