首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pester测试驱动器不存在

Pester测试驱动器不存在
EN

Stack Overflow用户
提问于 2020-08-28 00:11:39
回答 1查看 451关注 0票数 2

我正在尝试使用Pesters TestDrive为自定义文件管理powershell函数创建一个测试。但是,我没有以任何方式运行它,总是收到TestDrive不存在的错误。

即使使用文档中的示例:https://pester.dev/docs/usage/testdrive

我创建了一个文件"pester.tests.ps1“,其中只包含示例:

代码语言:javascript
复制
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"
    }
}

出现以下错误:

代码语言:javascript
复制
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。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-31 05:38:05

Pester v5最近发布了,对于Pester的操作方式来说,这是一个相当重要的变化,测试是提前解释的。因此,对于如何构建测试,有一些突破性的变化,其中之一是测试的设置需要通过beforeallbeforeeach块完成。

因此,重写你的例子是可行的:

代码语言:javascript
复制
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,我刚刚给它添加了一个注释,以指出它的文档示例不再有效的事实。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63620093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档