Skip to content

Cmdlet under test with parameter $ExecutionContext causes Internal Error. #303

@alx9r

Description

@alx9r

I have encountered this error:

Internal error detected:  Mock for 'Write-Error' in module '' was called, but does not exist in the mock table.
      at line: 733 in C:\oltkPS\modules\Pester\Functions\Mock.ps1

There's a comment on line 732 that says

If this ever happens, it's a bug in Pester. The scriptBlock that calls Invoke-Mock should be
removed at the same time as the entry in the mock table.

Repro

Running the following test reproduces the error. If you change $ExecutionContext to anything else the error seems to be avoided. It looks like Mock.ps1 uses $ExecutionContext internally.

module.psm1

Function InternalErrorRepro
{
    [CmdletBinding()]
    param
    (
        $ExecutionContext
    )
    process
    {
        Write-Warning 'warning message'
    }
}

module.Tests.ps1

Import-Module module

InModuleScope module {
Describe 'InternalErrorRepro' {
    Mock -Verifiable Write-Warning
    Context 'context' {
        It 'does stuff' {
            InternalErrorRepro 
            Assert-MockCalled Write-Warning
        }
    }
}
}

Workaround

Obviously, avoiding cmdlet parameters named $ExecutionContext is a workaround. I wonder whether there is the potential for other naming collisions as well.

Solution

Ideally the cmdlet parameters and the internals of Mock.ps1 would not share the same namespace. If that's can't be achieved, perhaps $ExecutionContext (and whatever other variables that share namespace with the cmdlet parameters) could be suffixed with some random characters to reduce the likelihood of a collision.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions