Context "Invalid From number supplied" {
Mock Invoke-WebRequest {
$message = '{"code": 21212, "message": "The ''From'' number is not a valid phone number, shortcode, or alphanumeric sender ID.", "more_info": "https://www.twilio.com/docs/errors/21212", "status": 400}'
$errorRecord = New-ErrorRecord 400 $message
Throw $errorRecord
} -ModuleName "PsTwilio"
It "throws 'Invalid 'From' Number [21212]'" {
{ Send-SmsMessage -From 'FROM' -To $to -Message $message -Credential $credential -ErrorAction Stop } | Should -Throw "Invalid 'From' number [21212]"
Assert-MockCalled "Invoke-WebRequest" -ModuleName "PsTwilio" -Exactly 1
}
} # /Context
I've refactored the code to create a New-ErrorRecord into a separate file (Tests\Helpers.ps1):
function New-ErrorRecord
{
param (
[string]$statusCode,
[string]$errorDetails
) ....
}
and dot sourced it at the beginning of the test:
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$projectRoot = Split-Path -Parent $here
Import-Module "$projectRoot/PsTwilio.psm1" -Force -ErrorAction Stop
. "$here/Helpers.ps1"
Unfortunately, when I run the tests:
PS> Invoke-Pester -tag unit
I get an error that suggests that it's not finding the helper file:
Context Invalid From number supplied
[-] throws 'Invalid 'From' Number [21212]' 185ms
Expected: the expression to throw an exception with message {Invalid 'From' number [21212]}, an exception was raised, message was {Exception of type 'Microsoft.PowerShell.Commands.WriteErrorException' was thrown.}
Oddly enough, if I open the testing script in VSCode and press F5, the tests work fine.
What's the recommended way to using and including helper files?