Skip to content

Conversation

@nohwnd
Copy link
Member

@nohwnd nohwnd commented Jan 23, 2026

Wanted to spend few minutes on this, since I always say that it would be nice to have. LMK what you think before I start polishing it.

I struggle with identifying which mock was or was not invoked, and why, especially when validating that some cmdlet is called with the correct parameters.

describe "a" {

    it "i" {
        Mock Get-Command {  }

        Get-Command -Name Hello
        Get-Command -Name How

        Write-host ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
        Should -Invoke Get-Command -Times 10
        write-host ( & (Get-Module Pester ) {
            } )
    }

    it "i2" {
        Mock Get-Command {  }

        Get-Command -Name Hello
        Get-Command -Name How

        Write-host ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
        Should -Invoke Get-Command -ParameterFilter { $name -eq "aaa" }
        write-host ( & (Get-Module Pester ) {
            } )
    }
}
image

It improves the situation a bit, but to make it really useful, maybe:

  • show which mock was invoked somehow (but how? they don't have their own identifiers)
  • show what is the filter on the mock we are looking for (it is there in the stack trace, but is that obvious?
  • change the way it is printed so it does not look like runnable code, when it might not be runnable
  • nice-format the parameters

@HeyItsGilbert
Copy link

HeyItsGilbert commented Jan 26, 2026

Would + and - in the front of the matching calls be better? Either way I think putting the identifier at the beginning may help if the commands are super long.

Another nice to have would be identifying where the call came from (e.g. line number, file, etc.). This could be helpful in identifying mocked calls that are similar but for some reason the logic branch didn't hit them.

I wonder (in another PR) if we flipped this on its head, and made it so we could evaluate what would be mocked (so read in the test file first) and then parsed a script and returned a list of mocked (with what criteria passed/failed). I know that would help me for sure. The other nice thing with this is that if we failed to properly mock, we would know that before running potentially system impacting commands.

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 this pull request may close these issues.

3 participants