-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to test for ShouldProcess and Confirm? #9
Comments
That's a tough one. I assume you have a function like this:
If you're writing a dangerous action, you probably want to write tests like:
In this case, you want to control the output of $PSCmdlet.ShouldProcess. I think the easiest way to do this is to create another function called CheckShouldProcess that just calls $PSCmdlet.ShouldProcess. Then you can use PSMock to mock the function and control the outputs. Something like this:
It might also be possible to replace $PSCmdlet with a new PSObject where you control the ShouldProcess method, but since $PSCmdlet is an automatic variable, it probably wouldn't be too easy. |
Indeed, mocking (the whole) $PSCmdlet variable seems to be a bit hard and might also interfere with other code using it. Thanks for your thoughts on this. Very helpful. In general: I've used the PSate, PShould combination for some days now and I really like it. Eventually it's fun to write tests for PowerShell modules. |
Also, if you have more advanced testing scenarios, you can use parameters in the Mock:
Enjoy. |
Any idea how to test for ShouldProcess and Confirm support using PSate? The whatif output can be checked with Should, but how to test Confirm?
The text was updated successfully, but these errors were encountered: