Skip to content

A mocking library for PowerShell, including dynamic mocks, multiple context levels, and call tracking.

License

Notifications You must be signed in to change notification settings

jonwagner/PSMock

Repository files navigation

PSMock

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:

Examples

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"

Features

See the PSMock wiki for full documentation.

  • Works standalone or with another test framework
  • Mocks any function or command
  • Conditional mocks with the -when parameter
  • Named cases can be added/removed
  • Mock contexts to automatically remove mocks
  • Call tracking at the mock and case level

Getting PSMock

A variety of ways:

Credits

PSMock was inspired by the great work by the Pester team. See PSMock v Pester on the wiki to learn about some differences.

About

A mocking library for PowerShell, including dynamic mocks, multiple context levels, and call tracking.

Resources

License

Stars

Watchers

Forks

Packages

No packages published