Mocking New-PSSession with Pester

Tagged: 

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Greg Tate Greg Tate 1 month, 1 week ago.

  • Author
    Posts
  • #64171
    Profile photo of Greg Tate
    Greg Tate
    Participant

    Hey Everyone,

    Looking for a trick to mock New-PSSession. The class doesn't have a constructor, so I can't use New-Object or New-MockObject. Do you have any tricks for mocking this command?

    Thanks!

  • #64173
    Profile photo of Don Jones
    Don Jones
    Keymaster

    I've played with this a lot – not sure its feasible. As you say, you can't directly construct the resulting session object, so you can't return a session. The only solution would be to mock whatever is using the session also, like Invoke-Command or whatever. That way you don't need a session. You basically mock all Remoting 🙂

    • #64176
      Profile photo of Greg Tate
      Greg Tate
      Participant

      It's a tough one. The only valid workaround I can find is from a cached searched page where Dave Wyatt explains to mock by creating a new session to localhost.

      Mock New-PSSession Using Localhost

      I'll go this route for now. If there is a better way, then this would be juicy info for the "Mocking the Unmockable" section!

      In my case I am trying to mock Invoke-Command. My implementation of Invoke-Command uses a session parameter, which Invoke-Command attempts to validate. Since I can't create a mock PSSession object, the parameter validation fails, which causes the mock of Invoke-Command to fail.

      Thanks for the quick response, Don!

You must be logged in to reply to this topic.