gpt4 book ai didi

powershell - 如何在 Pester 测试中模拟 Read-Host?

转载 作者:行者123 更新时间:2023-12-03 19:24:07 27 4
gpt4 key购买 nike

如果我有这个功能:

Function Test-Foo {

$filePath = Read-Host "Tell me a file path"
}

我如何模拟 Read-Host 以返回我想要的内容?例如我想做这样的事情(这是行不通的):

Describe "Test-Foo" {
Context "When something" {
Mock Read-Host {return "c:\example"}

$result = Test-Foo

It "Returns correct result" {
$result | Should Be "c:\example"
}
}
}

最佳答案

这种行为是正确的:

你应该把你的代码改成

Import-Module -Name "c:\LocationOfModules\Pester"

Function Test-Foo {
$filePath = Read-Host "Tell me a file path"
$filePath
}

Describe "Test-Foo" {
Context "When something" {
Mock Read-Host {return "c:\example"}

$result = Test-Foo

It "Returns correct result" { # should work
$result | Should Be "c:\example"
}
It "Returns correct result" { # should not work
$result | Should Be "SomeThingWrong"
}
}
}

关于powershell - 如何在 Pester 测试中模拟 Read-Host?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41284810/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com