Mocking Invoke-Command

Welcome Forums Pester Mocking Invoke-Command

Viewing 2 reply threads
  • Author
    Posts
    • #274209
      Participant
      Topics: 1
      Replies: 1
      Points: 12
      Rank: Member

      How would one go about properly mocking ‘Invoke-Command’ for this example?  This is just a part of my function that I’m trying to test at the moment, the second part removes the package if there is a match (calling ‘Invoke-Command’ a second time with a different script block)..

      The test

       

      Results in:
      Expected Invoke-Command to be called at least 1 times but was called 0 times

      As mentioned earlier, the full function calls ‘Invoke-Command’ a second time.  I’m trying to understand how I can Mock ‘Invoke-Command’ for the two different calls, or do I need to even further and Mock ‘choco’?

      The full function:

    • #274518
      Senior Moderator
      Topics: 9
      Replies: 1449
      Points: 5,426
      Helping Hand
      Rank: Community MVP

      you don’t need to wrap Remove-ChocoP -packageName $packageName1 -computerName $computerName inside {}. That is required only when testing exceptiions.
      And there is a typo as your function name is Remove-ChocoPackage but you typed it as Remove-ChocoP, but that was not caught as the error occurred inside {} context I think. Mocking works fine if you remove {} and correct the typo.

    • #274710
      Participant
      Topics: 1
      Replies: 1
      Points: 12
      Rank: Member

      Actually tried to remove all that after posting, was just a typo…

      With the typo fixed and the curly braces removed, still experienced issues with ‘invoke-command’.

      A couple of things needed to be resolved…

      First, realized I had my repo in a folder under ‘my documents’… moved this all to a shorter/simpler path…

      Also, ended up breaking out the first ‘invoke-command’ out into a private function, which I then mocked… which solved my issues…

Viewing 2 reply threads
  • The topic ‘Mocking Invoke-Command’ is closed to new replies.