Search code examples
unit-testingpowershelltestingmockingpester

Mock [System.IO.Path]::IsPathRooted() using Pester?


How do I Mock [System.IO.Path]::IsPathRooted() using Pester? I tried the following but no luck.

Describe "Configuration" {
    Mock [System.IO.Path]::IsPathRooted {
        return false
    }

    It "should mock rooted Path" {
        [System.IO.Path]::IsPathRooted("C:\a") | Should Be False
    }

Solution

  • Unfortunately the answer to your question is short and simple:

    You can't.


    According to Pester's Github Issue #72, the best alternative is to wrap the method call in a separate function and Mock this function.

    There are a few discussions about this and a lot of people would love to be able to mock .Net methods, but until now, I was unable to find any solution on this. Unfortunately, even overwriting methods (independent of Pester) isn't that easy.