gpt4 book ai didi

powershell - Pester Testdrive不存在

转载 作者:行者123 更新时间:2023-12-02 23:49:50 26 4
gpt4 key购买 nike

我正在尝试使用Pesters TestDrive为自定义文件管理Powershell函数创建测试。
但是,我没有让它以任何方式运行,总是会收到TestDrive不存在的错误。
即使使用文档中的示例:https://pester.dev/docs/usage/testdrive
我创建了一个仅包含示例的文件“pester.tests.ps1”:

function Add-Footer($path, $footer) {
Add-Content $path -Value $footer
}

Describe "Add-Footer" {
$testPath = "TestDrive:\test.txt"
Set-Content $testPath -value "my test text."
Add-Footer $testPath "-Footer"
$result = Get-Content $testPath

It "adds a footer" {
(-join $result) | Should -Be "my test text.-Footer"
}
}
错误出现:
Starting discovery in 1 files. Set-Content : Cannot find drive. A drive with the name 'TestDrive' does not exist. At ...\pester.tests.ps1:7 char:5
+ Set-Content $testPath -value "my test text."
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (TestDrive:String) [Set-Content], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SetContentCommand Add-Content : Cannot find drive. A drive with the name 'TestDrive' does not exist. At ...\pester.tests.ps1:2 char:5
+ Add-Content $path -Value $footer
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (TestDrive:String) [Add-Content], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.AddContentCommand Get-Content : Cannot find drive. A drive with the name 'TestDrive' does not exist. At ...\pester.tests.ps1:9 char:15
+ $result = Get-Content $testPath
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (TestDrive:String) [Get-Content], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetContentCommand
Discovery finished in 46ms. [-] Add-Footer.adds a footer 10ms (8ms|2ms) Expected strings to be the same, but they were different. Expected length: 20 Actual length: 0 Strings differ at index 0. Expected: 'my test text.-Footer' But was: '' at (-join $result) | Should -Be "my test text.-Footer", ...\pester.tests.ps1:12 at <ScriptBlock>, ...\pester.tests.ps1:12 Tests completed in 152ms Tests Passed: 0, Failed: 1, Skipped: 0 NotRun: 0
我忘记了什么吗?还有其他先决条件吗?我已经更新了Pester和Powershell。

最佳答案

Pester v5是最近发布的,它对Pester的操作方式进行了相当大的更改,并预先解释了测试。结果,对于您必须如何构建测试的方式进行了一些重大更改,其中之一就是需要通过beforeallbeforeeach块来完成测试的设置。
因此,您的示例的这种重写有效:

function Add-Footer($path, $footer) {
Add-Content $path -Value $footer
}

Describe "Add-Footer" {

BeforeAll {
$testPath = "TestDrive:\test.txt"
Set-Content $testPath -value "my test text."
}

It "adds a footer" {
Add-Footer $testPath "-Footer"
$result = Get-Content $testPath
(-join $result) | Should -Be "my test text.-Footer"
}
}
关于Pester v5如何影响TestDrive有一个 open issue,我刚刚在其中添加了一条注释,以指出该示例文档不再有效的事实。

关于powershell - Pester Testdrive不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63620093/

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