1

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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
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.)
0
(Resolve-Path ($PSScriptRoot + "\..\..\..\AOI\UDT\testfile.text"))

Use this. You will be able to use your file anywhere.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.