如何使用 Pester 验证传递给模拟函数的参数值
How can I verify the value of a parameter being passed into a mocked function using Pester
我有一个用 PowerShell 编写的函数,我正在尝试使用 Pester v-5 测试该函数。我在下面的代码示例中包含了被测函数和 Pester 测试用例。
我正在尝试验证传递给模拟函数 Save-Report
的参数 $FileName
是否正确。我该怎么做?
备注:
- 当我模拟函数
Add-StringToFileName
和 return 测试值时,我意识到 $Filename
参数的值被设置为测试值。但是我还是想测试下这个值到底能不能保持不变
- 这绝对是我想在未来的测试中做的事情,所以我想了解它是如何完成的。
测试中的函数
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
}
单元测试
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.
}
}
}
您可以确认使用特定参数调用了模拟,如下所示:
Should -Invoke -CommandName Save-Report -Times 1 -ParameterFilter { $Filename -eq "test_string_20210101" }
有关详细信息,请参阅 https://pester-docs.netlify.app/docs/usage/mocking#example。
我有一个用 PowerShell 编写的函数,我正在尝试使用 Pester v-5 测试该函数。我在下面的代码示例中包含了被测函数和 Pester 测试用例。
我正在尝试验证传递给模拟函数 Save-Report
的参数 $FileName
是否正确。我该怎么做?
备注:
- 当我模拟函数
Add-StringToFileName
和 return 测试值时,我意识到$Filename
参数的值被设置为测试值。但是我还是想测试下这个值到底能不能保持不变 - 这绝对是我想在未来的测试中做的事情,所以我想了解它是如何完成的。
测试中的函数
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
}
单元测试
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.
}
}
}
您可以确认使用特定参数调用了模拟,如下所示:
Should -Invoke -CommandName Save-Report -Times 1 -ParameterFilter { $Filename -eq "test_string_20210101" }
有关详细信息,请参阅 https://pester-docs.netlify.app/docs/usage/mocking#example。