Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$pester.ShouldExceptionLine on occasion doesn't exist #75

Closed
MarkMichaelis opened this issue Oct 13, 2013 · 3 comments
Closed

$pester.ShouldExceptionLine on occasion doesn't exist #75

MarkMichaelis opened this issue Oct 13, 2013 · 3 comments

Comments

@MarkMichaelis
Copy link

Caveat: I can't imagine this hasn't been found by others so I suspect I am doing something wrong, however, there seems to be a problem (given my usage) with It.ps1.

If I throw an error in my test (mistakenly or intentionally), it appears that Get-PesterResult fails with the following errors:

Property 'ShouldExceptionLine' cannot be found on this object. Make sure that it exists.
At C:\Chocolatey\lib\pester.2.0.3\tools\Functions\It.ps1:124 char:12
+         if($pester.ShouldExceptionLine) {
+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], PropertyNotFoundException
    + FullyQualifiedErrorId : PropertyNotFoundStrict

The variable '$line' cannot be retrieved because it has not been set.
At C:\Chocolatey\lib\pester.2.0.3\tools\Functions\It.ps1:131 char:57
+         $failureLine = $test.StartPosition.StartLine + ($line-2)
+                                                         ~~~~~
    + CategoryInfo          : InvalidOperation: (line:String) [], RuntimeException
    + FullyQualifiedErrorId : VariableIsUndefined

The variable '$failureLine' cannot be retrieved because it has not been set.
At C:\Chocolatey\lib\pester.2.0.3\tools\Functions\It.ps1:132 char:44
+         $testResult.stackTrace = "at line: $failureLine in $($test.File)"
+                                            ~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (failureLine:String) [], RuntimeException
    + FullyQualifiedErrorId : VariableIsUndefined

I believe the problem is that on occasion (perhaps never?), ShouldExceptionLine is not set. To avoid this I changed the conditional in the if statement from simply

    ...
    if($pester.ShouldExceptionLine) {
        $line=$pester.ShouldExceptionLine
        $pester.ShouldExceptionLine=$null
    }
    ...

to

    ...
    if($pester.ContainsKey("ShouldExceptionLine") -and $pester.ShouldExceptionLine) {
        $line=$pester.ShouldExceptionLine
        $pester.ShouldExceptionLine=$null
    }
    ...

I suspect an additional modification would be to set ShouldExceptionLine (possibly in the catch block on line 76 of It.ps1, but I am not certain) as follows:

    ...
    try{
        temp
    } catch {
        $pester.results.FailedTestsCount += 1;
        if ($_.Exception | Get-Member Line) { $pester.ShouldExceptionLine = $_.Exception.Line }
        $exception = $_;
    }
    ...

I wonder if the problem is due to a Set-StrictMode -Version "Latest" or Set-StrictMode -Version 3 entry, perhaps that is why others haven't noticed this?

Thanks,
Mark

@artbear
Copy link

artbear commented Jan 24, 2014

Add:
Text of my script

''''
Set-StrictMode -version 2.0

Describe "Test Set-StrictMode" {

It "Test it" {
    $true | Should Be $true
}

}
''''
Run "invoke-pester myScript.tests.ps1"
and :
Describing Test Set-StrictMode
The variable '$PesterException' cannot be retrieved because it has not been set.
At C:\Users\aayuhanov\Documents\WindowsPowerShell\Modules\Pester\Functions\It.ps1:89 char:60

  • $pester.testResult = Get-PesterResult $pester.ThisTest $PesterException
    
  •                                                        ~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (PesterException:String) [], RuntimeException
    • FullyQualifiedErrorId : VariableIsUndefined

Property 'testResult' cannot be found on this object. Make sure that it exists.
At C:\Users\aayuhanov\Documents\WindowsPowerShell\Modules\Pester\Functions\It.ps1:90 char:5

  • $pester.results.CurrentDescribe.Tests += $pester.testResult
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : NotSpecified: (:) [], PropertyNotFoundException
    • FullyQualifiedErrorId : PropertyNotFoundStrict

Property 'testResult' cannot be found on this object. Make sure that it exists.
At C:\Users\aayuhanov\Documents\WindowsPowerShell\Modules\Pester\Functions\It.ps1:108 char:8

  • if($pester.testResult.success) {
    
  •    ~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : NotSpecified: (:) [], PropertyNotFoundException
    • FullyQualifiedErrorId : PropertyNotFoundStrict

Tests completed in 0ms
Passed: 1 Failed: 0

@scottmuc
Copy link
Contributor

PowerShell 2.0 and 3.0 compatibility is sort of in an unknown state right now :-(

I'm currently getting a Windows VM sorted out so I can take a look at this. Thanks for the sample code and output, that'll help in figuring this out.

@cheretbe
Copy link

I'm also having this problem with Set-StrictMode.
Initially I was using PS 2.0 and hit this page with Google. I need my scripts to be PS 2.0 compatible, but it's no problem for me to use PS 4.0 for testing.
Unfortunately it doesn't work on PS 4.0 either.
With this test code, offered by artbear:

Set-StrictMode -version "latest"
Describe "Test Set-StrictMode" {
  It "Test it" {
    $true | Should Be $true
  }
}

I get the following output:

Executing all tests in C:\temp
Describing Test Set-StrictMode
The variable '$PesterException' cannot be retrieved because it has not been set.
At C:\Temp\pester\Functions\It.ps1:78 char:67
+     $Result = Get-PesterResult -Test $Test -Time $time -Exception $PesterExcepti ...
+                                                                   ~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (PesterException:String) [], RuntimeException
    + FullyQualifiedErrorId : VariableIsUndefined

The variable '$Result' cannot be retrieved because it has not been set.
At C:\Temp\pester\Functions\It.ps1:79 char:27
+     $Pester.AddTestResult($Result.name, $Result.Success, $result.time, $result.f ...
+                           ~~~~~~~
    + CategoryInfo          : InvalidOperation: (Result:String) [], RuntimeException
    + FullyQualifiedErrorId : VariableIsUndefined

Index was outside the bounds of the array.
At C:\Temp\pester\Functions\It.ps1:80 char:5
+     $Pester.testresult[-1] | Write-PesterResult
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], IndexOutOfRangeException
    + FullyQualifiedErrorId : System.IndexOutOfRangeException

Tests completed in 0ms
Passed: 0 Failed: 0

PowerShell version is 4.0, Pester version is 2.0.4 (March 9, 2014)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants