powershell - 需要有关 PowerShell 功能和模块的一些帮助

标签 powershell pester

我正在使用这个模块:

Function Get-BuildVersion {
param(
    [string] $MajorMinorVersion,
    [int] $BuildCounter,
    [int] $Offset=30,
    [string] $Suffix = "-hotfix"
 )

return "$MajorMinorVersion.$BuildCounter"
}

Export-ModuleMember -Function @(
"Get-BuildVersion"
)

我正在使用这个 Pester 脚本来测试:

$here = Convert-Path $PSScriptRoot
Import-Module $here\BuildNumber -Force

InModuleScope BuildNumber {

Describe "Get-BuildVersion is triggered" {
    $testCases = @(
        @{ MajorMinorVersion = "1.0"; BuildCounter = 0; ExpectedResult = "1.0.0" }
        @{ MajorMinorVersion = "2.0"; BuildCounter = 0; ExpectedResult = "2.0.0" }
        @{ MajorMinorVersion = "2.1"; BuildCounter = 0; ExpectedResult = "2.1.0" }
        @{ MajorMinorVersion = "2.1"; BuildCounter = 50; ExpectedResult = "2.1.50" }
        @{ MajorMinorVersion = "2.1"; BuildCounter = 50; Offset = 30; ExpectedResult = "2.1.80" }
        @{ MajorMinorVersion = "2.1"; BuildCounter = 50; Offset = 30; Suffix = "-hotfix"; ExpectedResult = "2.1.80-hotfix" }
    )

    Context "When getting build number" {
        foreach ($test in $testCases) {

            $result = Get-BuildVersion @test

            It "should return the expected version number: $($test.ExpectedResult)" {
                $result | Should Be $test.ExpectedResult
             }
         }
      }
   }
}

当我运行测试时,这是我得到的输出:

PS C:\users\Reddit\PowerShell> .\BuildNumber.Tests.ps1
Describing Get-BuildVersion is triggered
Context When getting build number
 [+] should return the expected version number: 1.0.0 60ms
 [+] should return the expected version number: 2.0.0 36ms
 [+] should return the expected version number: 2.1.0 36ms
 [+] should return the expected version number: 2.1.50 31ms
 [-] should return the expected version number: 2.1.80 45ms
   String lengths are both 6. Strings differ at index 4.
   Expected: {2.1.80}
   But was:  {2.1.50}
   ---------------^
   22:                     $result | Should Be $test.ExpectedResult
   at <ScriptBlock>, C:\users\Reddit\PowerShell\BuildNumber.Tests.ps1: line 22
 [-] should return the expected version number: 2.1.80-hotfix 74ms
   Expected string length 13 but was 6. Strings differ at index 4.
   Expected: {2.1.80-hotfix}
   But was:  {2.1.50}
   ---------------^
   22:                     $result | Should Be $test.ExpectedResult
   at <ScriptBlock>, C:\users\Reddit\PowerShell\BuildNumber.Tests.ps1: line 22

关于如何获得预期的 2.1.80 和 2.1.80-hotfix 值,有什么想法吗?我尝试过格式化字符串但无济于事。

我似乎无法弄清楚如何在不抛出参数的情况下传递其他变量 $Offset$Suffix

最佳答案

如果目的是让您的测试通过(作为一种测试驱动开发练习),您需要按如下方式修改您的函数:

Function Get-BuildVersion {
param(
    [string] $MajorMinorVersion,
    [int] $BuildCounter,
    [int] $Offset=0,
    [string] $Suffix
 )

return "$MajorMinorVersion.$($BuildCounter+$Offset)$Suffix"
}

您的测试应该按照当前定义全部通过。

更改:

  • $Offset 的默认值为 0,除非被提供的值覆盖。
  • $Suffix 没有默认值,因此默认为空。
  • 返回的字符串已修改为在其输出中包含这两个参数。

另外仅供引用,Return 关键字在技术上是多余的,您可以将其删除。

关于powershell - 需要有关 PowerShell 功能和模块的一些帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48546979/

相关文章:

c# - powershell cmdlet 的动态输出类型?

regex - PowerShell - 将正则表达式与 threadjob 输出匹配可以工作,但不会填充 $matches 变量

powershell - PSRemoteRegistry.psm1 未进行数字签名

powershell - 我的测试代码和功能代码是否需要位于同一目录中才能使 Pester Code Coverage 正常工作?

powershell - 在 Powershell 中处理数组(使用 Pester 进行测试)

PowerShell 嵌套此处字符串

powershell - 无法启动 Windows 文件夹外的程序

PowerShell:使用前/后 block 纠缠单元测试

powershell - 如何为使用动态参数的高级函数 cmdlet 创建包装器

powershell - Pester 没有捕捉到抛出的错误