具有Pester测试的PowerShell-未找到测试文件,也未提供脚本块

问题描述

尝试学习Pester(v5.0.4和PS v7.0.3)。我有这些文件

Get-Planet2.ps1

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

    $planets | Where-Object { $_.Name -like $Name }
}

Get-Planet2.Tests.ps1:

BeforeAll { 
    # This will bring the function from the main file back to scope.
    . $PSScriptRoot/Get-Planet2.ps1

    param(
        [parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string]$name,[parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string]$title,[parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string]$InputFile
    )
}

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

    It 'Earth is the third planet in our Solar System' {
        $allPlanets = Get-Planet
        $allPlanets[2].Name | Should -Be 'Earth'
    }

    It 'Pluto is not part of our Solar System' {
        $allPlanets = Get-Planet
        $plutos = $allPlanets | Where-Object Name -EQ 'Pluto'
        $plutos.Count | Should -Be 0
    }

    It 'Planets have this order: Mercury,Venus,Earth,Mars,Jupiter,Saturn,Uranus,Neptune' {
        $allPlanets = Get-Planet
        $planetsInOrder = $allPlanets.Name -join ',' 
        $planetsInOrder | Should -Be 'Mercury,Neptune'
    } 

}

Call-Get-Planet2.ps1:

$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$path = "$here/Get-Planet2.Tests.ps1"
$parameters = @{name = "John"; title = "Sales"; InputFile = "$here/test.json"}

write-host "Path: $path"
write-host $parameters

Invoke-Pester -Script @{Path=$path;Parameters=$parameters}

我的问题:

运行Call-Get-Planet2.ps1时,出现以下错误。我不知道为什么要这么做。需要指导。谢谢

System.Management.Automation.RuntimeException:没有找到测试文件,也没有提供脚本块。 在Invoke-Pester处,/ Users / myaccount / .local / share / powershell / Modules / Pester / 5.0.4 / Pester.psm1:第4520行 在,/Users/myaccount/repos/myrepo/Pester/Call-Get-Planet2.ps1:第8行 在,:第1行

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)