PSMock (pronounced "smock" or "puh-smock") is a mocking module for PowerShell.
PSMock can mock functions and commands, filter mocks, track calls, and manage multiple contexts from whatever program you are running. Works with dot-sourced code, scripts, and call blocks.
PSMock is part of the PSST PowerShell Suite for Testing:
Getting started:
Import-Module PSMock
A simple mock:
# Need this to automatically handle scope changes
Enable-Mock | iex
function Original { "original" }
Original # "original"
Mock Original { "mocked" }
Original # "mocked"
Mocking with cases:
Enable-Mock | iex
function Hello { param ([string] $who) "Hello, $who" }
Hello you # "Hello, you"
Mock Hello { } -when { $who -eq "Bob" }
Hello you # "Hello, you"
Hello bob # nothing
Mock Hello { "Good day, $who" }
Hello bob # nothing
Hello you # "Good day, you"
Call tracking:
Enable-Mock | iex
function Hello { param ([string] $who) "Hello, $who" }
Mock Hello { } -when { $who -eq "Bob" } -name Bob
Mock Hello { "Good day, $who" }
Hello bob # nothing
Hello you # "Good day, you"
(Get-Mock Hello).Count # 2
(Get-Mock Hello -case Bob).Count # 1
(Get-Mock Hello -case default).Count # 1
(Get-Mock Hello -case default).Calls[0].BoundParameters['who'] # "you"
See the PSMock wiki for full documentation.
A variety of ways:
PSMock was inspired by the great work by the Pester team. See PSMock v Pester on the wiki to learn about some differences.