首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Pester验证传递给模拟函数的参数的值?

如何使用Pester验证传递给模拟函数的参数的值?
EN

Stack Overflow用户
提问于 2021-09-22 08:30:00
回答 1查看 572关注 0票数 0

我有一个用PowerShell编写的函数,我正在尝试使用Pester v-5测试这个函数。我已经在下面的代码样本中包含了测试中的函数和Pester测试用例。

我试图验证传递给模拟函数Save-Report的参数Save-Report是否正确。我怎么能这么做?

备注:

  • 我认识到,当我模拟函数Add-StringToFileName并返回测试值时,$Filename参数的值将被设置为测试值。但是,我仍然想测试这个值是否使它始终保持不变,并且仍然是unchanged.
  • This,这是我在将来的测试中想要做的事情,所以我想了解它是如何实现的。

测试下的函数

代码语言:javascript
复制
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
}

单元测试

代码语言:javascript
复制
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.
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-22 09:00:02

您可以使用以下特定参数确认调用了模拟:

代码语言:javascript
复制
Should -Invoke -CommandName Save-Report -Times 1 -ParameterFilter { $Filename -eq "test_string_20210101" }

有关更多详细信息,请参阅https://pester-docs.netlify.app/docs/usage/mocking#example

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69280708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档