pester / docs

Source files for the Pester website.
https://pester.dev
MIT License
41 stars 77 forks source link

None of the quick starts work #153

Closed Strandedpirate closed 2 years ago

Strandedpirate commented 2 years ago

I must be missing something obvious. None of the quick start guides in these two locations work.

https://github.com/pester/Pester https://pester.dev/docs/quick-start

Output

PS F:\git\kanopy\Scripts> Invoke-Pester  .\Get-Planet.Tests.ps1

 [-] Error occurred in test script 'F:\git\kanopy\Scripts\Get-Planet.Tests.ps1' 17ms
   RuntimeException: The BeforeAll command may only be used inside a Describe block.
   at Assert-DescribeInProgress, C:\Program Files\WindowsPowerShell\Modules\Pester\3.4.0\Functions\Describe.ps1: line 125
   at BeforeAll, C:\Program Files\WindowsPowerShell\Modules\Pester\3.4.0\Functions\SetupTeardown.ps1: line 56
   at <ScriptBlock>, F:\git\kanopy\Scripts\Get-Planet.Tests.ps1: line 1
   at <ScriptBlock>, C:\Program Files\WindowsPowerShell\Modules\Pester\3.4.0\Pester.psm1: line 297
   at Invoke-Pester, C:\Program Files\WindowsPowerShell\Modules\Pester\3.4.0\Pester.psm1: line 310
   at <ScriptBlock>, <No file>: line 1
Tests completed in 17ms
Passed: 0 Failed: 1 Skipped: 0 Pending: 0 Inconclusive: 0

Get-Planet.Tests.ps1

BeforeAll {
    # your function
    function Get-Planet ([string]$Name='*')
    {
        $planets = @(
            @{ Name = 'Mercury' }
            @{ Name = 'Venus'   }
            @{ Name = 'Earth'   }
            @{ Name = 'Mars'    }
            @{ Name = 'Jupiter' }
            @{ Name = 'Saturn'  }
            @{ Name = 'Uranus'  }
            @{ Name = 'Neptune' }
        ) | foreach { [PSCustomObject]$_ }

        $planets | where { $_.Name -like $Name }
    }
}

# Pester tests
Describe 'Get-Planet' {
  It "Given no parameters, it lists all 8 planets" {
    $allPlanets = Get-Planet
    $allPlanets.Count | Should -Be 8
  }

  Context "Filtering by Name" {
    It "Given valid -Name '<Filter>', it returns '<Expected>'" -TestCases @(
      @{ Filter = 'Earth'; Expected = 'Earth' }
      @{ Filter = 'ne*'  ; Expected = 'Neptune' }
      @{ Filter = 'ur*'  ; Expected = 'Uranus' }
      @{ Filter = 'm*'   ; Expected = 'Mercury', 'Mars' }
    ) {
      param ($Filter, $Expected)

      $planets = Get-Planet -Name $Filter
      $planets.Name | Should -Be $Expected
    }

    It "Given invalid parameter -Name 'Alpha Centauri', it returns `$null" {
      $planets = Get-Planet -Name 'Alpha Centauri'
      $planets | Should -Be $null
    }
  }
}
Strandedpirate commented 2 years ago

If I do what the error suggests and re-run, I get another slew of errors.

PS F:\git\kanopy\Scripts> Invoke-Pester .\Get-Planet.Tests.ps1

Describing Get-Planet
 [-] Given no parameters, it lists all 8 planets 494ms
   RuntimeException: '-Be' is not a valid Should operator.
   at Get-TestResult, C:\Program Files\WindowsPowerShell\Modules\Pester\3.4.0\Functions\Assertions\Should.ps1: line 42
   at <ScriptBlock>, F:\git\kanopy\Scripts\Get-Planet.Tests.ps1: line 26
   Context Filtering by Name
    [-] Given valid -Name 'Earth', it returns 'Earth' 206ms
      RuntimeException: '-Be' is not a valid Should operator.
      at Get-TestResult, C:\Program Files\WindowsPowerShell\Modules\Pester\3.4.0\Functions\Assertions\Should.ps1: line 42
      at <ScriptBlock>, F:\git\kanopy\Scripts\Get-Planet.Tests.ps1: line 39
    [-] Given valid -Name 'ne*', it returns 'Neptune' 61ms
      RuntimeException: '-Be' is not a valid Should operator.
      at Get-TestResult, C:\Program Files\WindowsPowerShell\Modules\Pester\3.4.0\Functions\Assertions\Should.ps1: line 42
      at <ScriptBlock>, F:\git\kanopy\Scripts\Get-Planet.Tests.ps1: line 39
    [-] Given valid -Name 'ur*', it returns 'Uranus' 82ms
      RuntimeException: '-Be' is not a valid Should operator.
      at Get-TestResult, C:\Program Files\WindowsPowerShell\Modules\Pester\3.4.0\Functions\Assertions\Should.ps1: line 42
      at <ScriptBlock>, F:\git\kanopy\Scripts\Get-Planet.Tests.ps1: line 39
    [-] Given valid -Name 'm*', it returns 'Mercury Mars' 84ms
      RuntimeException: '-Be' is not a valid Should operator.
      at Get-TestResult, C:\Program Files\WindowsPowerShell\Modules\Pester\3.4.0\Functions\Assertions\Should.ps1: line 42
      at <ScriptBlock>, F:\git\kanopy\Scripts\Get-Planet.Tests.ps1: line 39
    [-] Given invalid parameter -Name 'Alpha Centauri', it returns $null 90ms
      RuntimeException: '-Be' is not a valid Should operator.
      at Get-TestResult, C:\Program Files\WindowsPowerShell\Modules\Pester\3.4.0\Functions\Assertions\Should.ps1: line 42
      at <ScriptBlock>, F:\git\kanopy\Scripts\Get-Planet.Tests.ps1: line 44
Tests completed in 1.02s
Passed: 0 Failed: 6 Skipped: 0 Pending: 0 Inconclusive: 0
Strandedpirate commented 2 years ago

Ok, not an issue. I figured it out. I just built a new system a few months back and kept the old hard drive in the computer. I must have installed Pester a few years back on that old drive and somehow the new windows install picked up the old module.? ¯_(ツ)_/¯.

So basically I was running on 3.4. Powershell refused to uninstall the old module, denies that it even existed. So I just renamed the old folder and installed the newest per docs.

C:\Program Files\WindowsPowerShell\Modules\Pester.old