我有一个用PowerShell编写的函数,我正在尝试使用Pester v-5测试这个函数。我已经在下面的代码样本中包含了测试中的函数和Pester测试用例。
我试图验证传递给模拟函数Save-Report的参数Save-Report是否正确。我怎么能这么做?
备注:
Add-StringToFileName并返回测试值时,$Filename参数的值将被设置为测试值。但是,我仍然想测试这个值是否使它始终保持不变,并且仍然是unchanged.测试下的函数
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.
}
}
}发布于 2021-09-22 09:00:02
您可以使用以下特定参数确认调用了模拟:
Should -Invoke -CommandName Save-Report -Times 1 -ParameterFilter { $Filename -eq "test_string_20210101" }有关更多详细信息,请参阅https://pester-docs.netlify.app/docs/usage/mocking#example。
https://stackoverflow.com/questions/69280708
复制相似问题