Search code examples
powershellunit-testingpester

Global Mock with Pester PowerShell


I have used PesterHelpers to construct a suite of tests for my module and have begun adding Functional tests. I run the min, norm, and full test scripts as needed to test my work. I find that I am using the same mocks over and over, copying them from script to script. Is it possible to create one global mock that all test scripts can use in both Public and Private directories?


Solution

  • You could probably put your mocks in to another file and dot source them in to your script:

    . .\mocks.ps1
    

    This would save some duplication in your scripts, but would also make them a little more obscure.

    I don’t think there’s any concept in Pester for declaring Mocks in a more global way, as I believe they are scoped to each describe or context block they are declared in.