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?
Asked
Active
Viewed 294 times
2 Answers
3
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.

Mark Wragg
- 22,105
- 7
- 39
- 68
-
Thanks! I am using VSCode and it provides a nice "Run Tests | Debug Tests" in the UI. I always fight with PowerShell paths. If I create this relative to the main test script, the individual tests don't resolve the path correctly. Do you know how I can get my cake and eat it too? – Matthew McDermott Jan 14 '19 at 20:54
-
The general practice is to use $PSScriptRoot which will be the path where the script resides, then go relative path from there (easy if it’s same directory, if it’s directory above then you can use \.. etc.) – Mark Wragg Jan 14 '19 at 21:05
0
(Resolve-Path ($PSScriptRoot + "\..\..\..\AOI\UDT\testfile.text"))
Use this. You will be able to use your file anywhere.

Radhik Patel
- 11
- 4