脚本在pester测试中返回$ null的脚本,而它不应该

发布于 2025-01-17 10:36:11 字数 2080 浏览 3 评论 0原文

所以我在powershell中有点问题 我为一个关于佩斯特测试的课堂项目写了一个简单的脚本 现在看起来

param ([Parameter(Mandatory, ValueFromPipeline = $true)]$FN, [Parameter(Mandatory)]$OP, [Parameter()]$SN)

$solution

try {
    if ($OP -eq '+') {

        $solution = ([long]($FN) + [long]($SN))
    
    }

    elseif ($OP -eq '-') {

        $solution = ([long]($FN) - [long]($SN))
    
    }

    elseif ($OP -eq '*') {

        $solution = ([long]($FN) * [long]($SN))
    
    }

    elseif ($OP -eq '/') {

        if ($SecondNumber -eq 0) {
            $solution = "Cannot divide by 0";
        }

        $solution = ([long]($FN) / [long]($SN))
    
    }

    elseif ($OP -eq 'V') {
        $solution = ([math]::Sqrt($FN))
    
    }
    
    else {
        $solution = "Not a valid operator"
    }
    
}
catch {
    Write-Output "An error occured"
}

return $solution

这一切都很好,当我进入控制台并使用oOutput进入变量时,它可以正常工作。

PS C:\PowerShellProject> $test = .\Script.ps1 1 + 1
PS C:\PowerShellProject> $test
2

但是,当我运行佩斯特测试时,

Describe "Test for Script.ps1" {
    Context "Testing calculations" {
        It "+ test" {
            $plus = .\Test.ps1 -FN 2 -OP + -SN 2
            $plus | Should -Be 4
        }
    }
}

它将返回以下内容

PS C:\PowerShellProject> Invoke-Pester .\Test.ps1

Starting discovery in 1 files.
Discovery found 1 tests in 15ms.
Running tests.

[-] Test for Script.ps1.Testing calculations.+ test 18ms (16ms|2ms)
 Expected 4, but got $null.
 at $plus | Should -Be 4, C:\PowerShellProject\Test.ps1:6
 at <ScriptBlock>, C:\PowerShellProject\Test.ps1:6
[-] Context Test for Script.ps1.Testing calculations failed
 InvalidOperationException: Collection was modified; enumeration operation may not execute.

Tests completed in 156ms
Tests Passed: 0, Failed: 1, Skipped: 0 NotRun: 1
BeforeAll \ AfterAll failed: 1
  - Test for Script.ps1.Testing calculations

,我对Powershell仍然很新,所以我一定在这里犯了一个愚蠢的错误。

也许有人可以将我重新定向到一个有类似问题的问题,或者给我解决问题的解决方案。这真的很有帮助,因为我已经在几个小时中摔断了这个小时,而且确实开始让我烦恼。

So I'm having a bit of an issue in PowerShell
I wrote a simple script for a class project about Pester testing
It looks like this

param ([Parameter(Mandatory, ValueFromPipeline = $true)]$FN, [Parameter(Mandatory)]$OP, [Parameter()]$SN)

$solution

try {
    if ($OP -eq '+') {

        $solution = ([long]($FN) + [long]($SN))
    
    }

    elseif ($OP -eq '-') {

        $solution = ([long]($FN) - [long]($SN))
    
    }

    elseif ($OP -eq '*') {

        $solution = ([long]($FN) * [long]($SN))
    
    }

    elseif ($OP -eq '/') {

        if ($SecondNumber -eq 0) {
            $solution = "Cannot divide by 0";
        }

        $solution = ([long]($FN) / [long]($SN))
    
    }

    elseif ($OP -eq 'V') {
        $solution = ([math]::Sqrt($FN))
    
    }
    
    else {
        $solution = "Not a valid operator"
    }
    
}
catch {
    Write-Output "An error occured"
}

return $solution

Now this all works fine and when i go into the console and run it with ooutput going into a variable it works fine.

PS C:\PowerShellProject> $test = .\Script.ps1 1 + 1
PS C:\PowerShellProject> $test
2

But then when i run this Pester test

Describe "Test for Script.ps1" {
    Context "Testing calculations" {
        It "+ test" {
            $plus = .\Test.ps1 -FN 2 -OP + -SN 2
            $plus | Should -Be 4
        }
    }
}

It returns the following

PS C:\PowerShellProject> Invoke-Pester .\Test.ps1

Starting discovery in 1 files.
Discovery found 1 tests in 15ms.
Running tests.

[-] Test for Script.ps1.Testing calculations.+ test 18ms (16ms|2ms)
 Expected 4, but got $null.
 at $plus | Should -Be 4, C:\PowerShellProject\Test.ps1:6
 at <ScriptBlock>, C:\PowerShellProject\Test.ps1:6
[-] Context Test for Script.ps1.Testing calculations failed
 InvalidOperationException: Collection was modified; enumeration operation may not execute.

Tests completed in 156ms
Tests Passed: 0, Failed: 1, Skipped: 0 NotRun: 1
BeforeAll \ AfterAll failed: 1
  - Test for Script.ps1.Testing calculations

I'm still pretty new to Powershell so I must probably be making a silly mistake here.

Maybe someone could redirect me to another question that had a similar problem or give me a solution to my problem. This would be really helpful since I have been breaking my head over this for a couple of hours and it's really starting to annoy me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

通知家属抬走 2025-01-24 10:36:11

问题是在您的函数开始时的这一行:

$solution

这不会正如您预期的那样声明变量,但是由于PowerShells隐式输出行为,它实际上是 outputs the varible。输出为$ null,因为尚未定义该变量。

为了实际定义变量您必须分配一个值:

$solution = 0

稍后,当您进行返回$ solude时,您实际上是在输出流中添加另一个值,当将其捕获到变量时,将是数组@($ null,2)

证明:

$plus = .\Test.ps1 -FN 2 -OP + -SN 2            
Write-Host $plus.GetType().Name

这将打印对象[],尽管我们期望int

在PowerShell中,您通常不会使用返回语句,除非您想早日退出功能。 返回$解决方案实际上只是一个快捷方式:

$solution  # implicit output
return     # exit from function

佩斯特测试脚本中还有另一个问题,您正在调用错误的脚本。应该是:

$plus = .\Script.ps1 -FN 2 -OP + -SN 2

The problem is this line at the start of your function:

$solution

This does not declare the variable, as you propably expect, but due to PowerShells implicit output behaviour, it actually outputs the variable. The output is $null as the variable has not been defined yet.

To actually define the variable you have to assign a value:

$solution = 0

Later, when you do return $solution, you are actually adding another value to the output stream, which when captured to a variable, will be the array @($null, 2).

Proof:

$plus = .\Test.ps1 -FN 2 -OP + -SN 2            
Write-Host $plus.GetType().Name

This prints Object[], although we would expect int.

In PowerShell you usually don't use the return statement, except when you want to early exit from a function. return $solution is actually just a shortcut for:

$solution  # implicit output
return     # exit from function

There is another issue in your Pester test script, where you are calling the wrong script. It should be:

$plus = .\Script.ps1 -FN 2 -OP + -SN 2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文