我正在尝试使用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。
发布于 2020-08-31 05:38:05
Pester v5最近发布了,对于Pester的操作方式来说,这是一个相当重要的变化,测试是提前解释的。因此,对于如何构建测试,有一些突破性的变化,其中之一是测试的设置需要通过beforeall或beforeeach块完成。
因此,重写你的例子是可行的:
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,我刚刚给它添加了一个注释,以指出它的文档示例不再有效的事实。
https://stackoverflow.com/questions/63620093
复制相似问题