脚本在pester测试中返回$ null的脚本,而它不应该
所以我在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是在您的函数开始时的这一行:
这不会正如您预期的那样声明变量,但是由于PowerShells隐式输出行为,它实际上是 outputs the varible。输出为
$ null
,因为尚未定义该变量。为了实际定义变量您必须分配一个值:
稍后,当您进行
返回$ solude
时,您实际上是在输出流中添加另一个值,当将其捕获到变量时,将是数组@($ null,2)
。证明:
这将打印
对象[]
,尽管我们期望int
。在PowerShell中,您通常不会使用
返回
语句,除非您想早日退出功能。返回$解决方案
实际上只是一个快捷方式:佩斯特测试脚本中还有另一个问题,您正在调用错误的脚本。应该是:
The problem is this line at the start of your function:
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:
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:
This prints
Object[]
, although we would expectint
.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:There is another issue in your Pester test script, where you are calling the wrong script. It should be: