Mocking New-PSSession with Pester

This topic contains 2 replies, has 2 voices, and was last updated by  Greg Tate 1 year, 5 months ago.

  • Author
  • #64171

    Greg Tate

    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?


  • #64173

    Don Jones

    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

      Greg Tate

      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.