如果我有这个功能:
Function Test-Foo {
$filePath = Read-Host "Tell me a file path"
}
我如何模拟读取主机以返回我想要的内容?例如我想做这样的事情(这不起作用):
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"
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)