I am testing a script in Powershell using pester, however I can't see if the script works or not because I keep getting access denied to certain files. Is there anyway to bypass this just so I can see if my script works or not? Thanks
Edit: my script is for automatically recycling any files not accessed in 90 days
The best way to handle this sort of thing with Pester is to use a Mock
of the cmdlet that is throwing permission denied to simulate its behaviour and still validate the rest of the script.
Alternatively, work on files that you know you have permissions to, ideally within the automatically created TestDrive:
space (which Pester cleans up at the end of testing). E.g within your test script, use New-Item
to create some files under TestDrive:
and then have your script operate on those files.
It's typically not desirable to have your script execute its actual behaviour as part of testing, particularly if that actual behaviour is destructive or otherwise state-changing. Using Mock
or TestDrive:
allows you to create a fixed scenario to test against.