Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 有没有一种方法可以使用Pester测试/断言写主机的输出?_Powershell_Pester - Fatal编程技术网

Powershell 有没有一种方法可以使用Pester测试/断言写主机的输出?

Powershell 有没有一种方法可以使用Pester测试/断言写主机的输出?,powershell,pester,Powershell,Pester,我正在为一个相当复杂的脚本编写测试,脚本中有一个特定的函数,它将向用户输出不同系列的日志消息。我想断言是否正在显示特定的日志消息 主要问题是,我不知道哪个参数隐式处理传递给Write-Hostcmdlet的文本 下面是一些代码,它捕获了我要做的事情的前提 测试功能 纠缠试验 输出 描述TestFunction [-]如果1或3超过19ms,则应使用适当的消息调用写入主机 在,:第235行 235:Assert MockCalled Write Host-justice 1-Scope It-Pa

我正在为一个相当复杂的脚本编写测试,脚本中有一个特定的函数,它将向用户输出不同系列的日志消息。我想断言是否正在显示特定的日志消息

主要问题是,我不知道哪个参数隐式处理传递给
Write-Host
cmdlet的文本

下面是一些代码,它捕获了我要做的事情的前提

测试功能 纠缠试验 输出
描述TestFunction
[-]如果1或3超过19ms,则应使用适当的消息调用写入主机
在,:第235行
235:Assert MockCalled Write Host-justice 1-Scope It-ParameterFilter{“TestInput包含1”}
应准确调用写入主机1次,但被调用2次
测试在106ms内完成
测试通过:0、失败:1、跳过:0、挂起:0、不确定:0
查看cmdlet后,我发现有一个
-Object
参数。这将接受要写入控制台的通用对象数组。
-Object
参数是需要在Pester测试的
-ParameterFilter
中指定的参数,以便断言正在显示适当的文本

我的更新代码如下

测试功能 纠缠试验 输出
-乘以2-准确地说
,也是出于某种奇怪的原因-作用域只有当
区分大小写时,它才对我正确工作,否则
它也在计算来自其他It块的结果
function TestFunction($TestInput) {
    if ($TestInput -contains 1) {
        Write-Host "TestInput contains a 1"
    }

    if ($TestInput -contains 3 ) {
        Write-Host "TestInput contains a 3"
    }

    if ($TestInput -contains 4 ) {
        Write-Host "TestInput contains a 4"
    }
}
Describe "TestFunction" {
    It "should call Write-Host with appropriate message if 1 or 3 is passed" {
        Mock Write-Host {}
        TestFunction -TestInput @(1, 2, 3)
        Assert-MockCalled Write-Host -Exactly 2 -Scope It
        Assert-MockCalled Write-Host -Exactly 1 -Scope It -ParameterFilter { "TestInput contains a 1" }
        Assert-MockCalled Write-Host -Exactly 1 -Scope It -ParameterFilter { "TestInput contains a 3" }
    }
}
  Describing TestFunction
    [-] should call Write-Host with appropriate message if 1 or 3 is passed 19ms
      at <ScriptBlock>, : line 235
      235:         Assert-MockCalled Write-Host -Exactly 1 -Scope It -ParameterFilter { "TestInput contains a 1" }
      Expected Write-Host to be called 1 times exactly but was called 2 times
Tests completed in 106ms
Tests Passed: 0, Failed: 1, Skipped: 0, Pending: 0, Inconclusive: 0 
function TestFunction($TestInput) {
    if ($TestInput -contains 1) {
        Write-Host "TestInput contains a 1"
    }

    if ($TestInput -contains 3 ) {
        Write-Host "TestInput contains a 3"
    }

    if ($TestInput -contains 4 ) {
        Write-Host "TestInput contains a 4"
    }
}
Describe "TestFunction" {
    It "should call Write-Host with appropriate message if 1 or 3 is passed" {
        Mock Write-Host {}
        TestFunction -TestInput @(1, 2, 3)
        Assert-MockCalled Write-Host -Exactly 2 -Scope It
        Assert-MockCalled Write-Host -Exactly 1 -Scope It -ParameterFilter { $Object -eq "TestInput contains a 1" }
        Assert-MockCalled Write-Host -Exactly 1 -Scope It -ParameterFilter { $Object -eq "TestInput contains a 3" }
    }
}
  Describing TestFunction
    [+] should call Write-Host with appropriate message if 1 or 3 is passed 20ms
Tests completed in 99ms
Tests Passed: 1, Failed: 0, Skipped: 0, Pending: 0, Inconclusive: 0