Correct way to reference a helper function

116 views
Skip to first unread message

craig buchanan

unread,
Mar 19, 2018, 6:25:25 PM3/19/18
to Pester
I'm hoping to refactor code to create into a helper function.

So, this:

Describe "Send-SmsMessage" -Tag 'unit' {


 
Context "Invalid From number supplied" {

 
Mock Invoke-WebRequest {
 $errorDetails
= '{"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}'
 $statusCode
= 400

 $response
= New-Object System.Net.Http.HttpResponseMessage $statusCode
 $exception
= New-Object Microsoft.PowerShell.Commands.HttpResponseException "$statusCode ($($response.ReasonPhrase))", $response
 
 $errorCategory
= [System.Management.Automation.ErrorCategory]::InvalidOperation
 
 $errorID
= 'WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand'
 $targetObject
= $null
 
 $errorRecord
= New-Object Management.Automation.ErrorRecord $exception, $errorID, $errorCategory, $targetObject
 $errorRecord
.ErrorDetails = $errorDetails
 $errorRecord

 
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


 
...


} # /Describe

becomes this:

 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?

Dave Wyatt

unread,
Mar 19, 2018, 10:39:26 PM3/19/18
to craig buchanan, Pester
Your helper functions were dot-sourced into the test script, but your mock exists in the psTwillo module, which can't see that scope.  I generally don't use Mock's -ModuleName parameter at all; I use InModuleScope instead.  Then I would put the statement that dot-sources the helper functions inside the InModuleScope block, so they'd be visible from any mocks defined there.

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
Message has been deleted
0 new messages