I have a function written in PowerShell and I am trying to test that function using Pester v-5. I have included the function under test and the Pester test case in the code samples below.
I am trying to verify that the parameter $FileName
being passed into the mocked function Save-Report
is correct. How can I do that?
Notes:
- I realise the value of that
$Filename
parameter is being set to a test value when I mock the functionAdd-StringToFileName
and return a test value. But I still want to test that the value makes it down to the end and remains unchanged. - This is definitely something that I would like to do in future tests so I want to understand how it can be done.
Function under test
Function Invoke-ReportDownloader
{
[CmdletBinding()]
Param
(
# The command object used to connect to the database
[Parameter(Mandatory)]
[System.Data.Odbc.OdbcCommand]$Command,
# An XML element containing the report item to be retrieved
[Parameter(Mandatory)]
[System.Xml.XmlElement]$ReportItem,
# Hashtable containing the configuration parameters
[Parameter(Mandatory)]
[hashtable]$ConfigParams,
# The SEMO participant that the report is being downloaded for
[Parameter(Mandatory)]
[System.Data.DataRow]$Participant
)
# If report is not supported jump to next report
$ReportName = $ReportItem.ReportName
if (-not (Test-ReportSupported $Command $ReportName)) {
$Logger.Warning("Report: $($ReportName) is not currently supported")
return
}
# (Kanban-7940) If there is no date included in the specified file name in
# the report item then we append the associated date to the filename. This
# is to avoid multiple reports of the same type overwriting each other.
$Filename = $ReportItem.FileName
if (-not (Test-TimestampIncluded -Filename $Filename)) {
$FileName = Add-StringToFileName -Path $Filename -SubString $ReportItem.Date
}
# If report already exists in database jump to next report
if (Test-MarketReportInDatabase $Command $ReportItem) {
return
}
Get-Report $ConfigParams $Participant $ReportItem
# If the response contains failure information throw an exception
$ReportSuccessParams = @{
ResponsePath = $ConfigParams.BmResponsePath
BaseName = $ConfigParams.ReportFilter
}
if (-not (Test-SuccessfulResponse @ReportSuccessParams)) {
throw "Unsuccessful response from BM"
}
$SaveReportParams = @{
RawPath = $ConfigParams.RawPath
XmlResponsePath = $ConfigParams.XmlResponsePath
Filename = $FileName
}
Save-Report @SaveReportParams
}
Unit tests
Describe 'Invoke-ReportDownloader' {
BeforeAll {
# Set up test $Cmd object
# Set up test $Logger object
# Set up test $ReportItem object
# Set up test $ConfigParams object
}
Context "No timestamp included in the filename" {
BeforeAll {
Mock Test-ReportSupported { return $true }
Mock Test-MarketReportInDatabase { return $false }
Mock Get-Report
Mock Test-SuccessfulResponse { return $true }
Mock Save-Report
}
BeforeEach {
$Script:ReportDownloaderParams = @{
Command = $Script:Cmd
ReportItem = $ReportItem
ConfigParams = $Script:ConfigParams
Participant = $Script:Participant
}
}
It "Should save the report with the timestamp included in the filename" {
$TestFileName = "test_string_20210101"
Mock Add-StringToFileName { return $TestFileName }
Invoke-ReportDownloader @Script:ReportDownloaderParams
Should -InvokeVerifiable Save-Report
# I want to verify here that the parameter $FileName being passed into the mocked function Save-Report is correct.
}
}
}
CodePudding user response:
You can confirm a mock was called with specific parameters as follows:
Should -Invoke -CommandName Save-Report -Times 1 -ParameterFilter { $Filename -eq "test_string_20210101" }
See https://pester-docs.netlify.app/docs/usage/mocking#example for more details.