简体   繁体   中英

Pester Testdrive does not exists

I'm trying to create a test for a custom file-management-powershell function using Pesters TestDrive. However, I'm not getting it running in any way, always receiving the error that TestDrive does not exists.

Even with the example from the documentation: https://pester.dev/docs/usage/testdrive

I created a file "pester.tests.ps1" with only the example in it:

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"
    }
}

The error appearing:

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

Am I forgetting something? Are there other prerequisites? I already updated both Pester and Powershell.

Pester v5 was recently released and it was quite a significant change for how Pester operates, with tests being interpreted in advance. As a result there's some breaking changes for how you have to structure your test one of which being that setup for your tests needs to be done via a beforeall or beforeeach block.

As such, this rewrite of your example works:

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"
    }
}

There is an open issue about how Pester v5 impacts TestDrive and i've just added a note to it to call out the fact the documentation example for it no longer works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM