Skip to content

Verifiable Mock overridden in It block causes Should -InvokeVerifiable to fail #2672

@SESEric

Description

@SESEric

Checklist

What is the issue?

I’m seeing what looks like an issue with how -Verifiable mocks behave when they are overridden in a narrower scope.

Summary

If a mock is defined as -Verifiable in an outer scope (e.g., Describe/Context) and then a different mock for the same command is defined in an inner scope (e.g., inside an It block) also marked -Verifiable, the inner mock is correctly used for the call, but Should -InvokeVerifiable fails.

If I remove -Verifiable from the outer mock, then overriding the mock in It works and Should -InvokeVerifiable passes.

This makes it difficult to have a default verifiable mock for most tests, while overriding it for specific tests.

Workaround

If I remove -Verifiable from the outer mock (leave it only on the inner mock), the override test passes.

Thank you.

Expected Behavior

When a mock is overridden in a narrower scope:

The overridden (inner) verifiable mock should be the one that needs to be “verified”.

The outer verifiable mock should either be considered shadowed/ignored for verification purposes, or automatically treated as not applicable in that It run.

Therefore, Should -InvokeVerifiable should pass when the effective mock(s) for the test were invoked.

Actual behavior

The call goes to the overridden mock in It (confirmed by return value / debug output).

Should -InvokeVerifiable fails anyway, apparently because the outer -Verifiable mock is still tracked as “not invoked”, even though it was overridden and could never be invoked in that test.

Steps To Reproduce

Minimal reproduction
Describe 'Verifiable mock overriding' {

BeforeAll {
    function Get-Data { 'real' }
}

Context 'default mock in outer scope' {

    It 'uses default and passes' {
        Get-Data | Should -Be 'outer-mock'
        Should -InvokeVerifiable
    }

    It 'overrides mock inside It but InvokeVerifiable fails' {

        # Override for this specific test
        Mock Get-Data { 'inner-mock' } -Verifiable

        # Confirm the override is actually used
        Get-Data | Should -Be 'inner-mock'

        # This fails (even though the inner verifiable mock was invoked)
        Should -InvokeVerifiable
    }
}

}

Describe your environment

No response

Possible Solution?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions