PageSourceSearch

https://pester.dev/assets/js/dded8a7d.5dbddcda.js

js pester.dev collected 2026-10-03 19:38:42 UTC 17,633 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunkpester_docs=self.webpackChunkpester_docs||[]).push([["4469"],{6367(e,n,l){l.r(n),l.d(n,{metadata:()=>a,default:()=>c,frontMatter:()=>r,contentTitle:()=>s,toc:()=>m,assets:()=>o});var a=JSON.parse('{"id":"commands/Mock","title":"Mock","description":"Help for Pester command \'Mock\'. Mocks the behavior of an existing command with an alternate implementation.","source":"@site/versioned_docs/version-v5/commands/Mock.mdx","sourceDirName":"commands","slug":"/commands/Mock","permalink":"/docs/v5/commands/Mock","draft":false,"unlisted":false,"editUrl":null,"tags":[],"version":"v5","frontMatter":{"id":"Mock","title":"Mock","description":"Help for Pester command \'Mock\'. Mocks the behavior of an existing command with an alternate implementation.","keywords":["PowerShell","Pester","Help","Documentation"],"hide_title":false,"hide_table_of_contents":false,"custom_edit_url":null},"sidebar":"commands","previous":{"title":"It","permalink":"/docs/v5/commands/It"},"next":{"title":"New-Fixture","permalink":"/docs/v5/commands/New-Fixture"}}'),t=l(1987),i=l(7008);let r={id:"Mock",title:"Mock",description:"Help for Pester command 'Mock'. Mocks the behavior of an existing command with an alternate implementation.",keywords:["PowerShell","Pester","Help","Documentation"],hide_title:!1,hide_table_of_contents:!1,custom_edit_url:null},s,o={},m=[{value:"SYNOPSIS",id:"synopsis",level:2},{value:"SYNTAX",id:"syntax",level:2},{value:"DESCRIPTION",id:"description",level:2},{value:"EXAMPLES",id:"examples",level:2},{value:"EXAMPLE 1",id:"example-1",level:3},{value:"EXAMPLE 2",id:"example-2",level:3},{value:"EXAMPLE 3",id:"example-3",level:3},{value:"EXAMPLE 4",id:"example-4",level:3},{value:"EXAMPLE 5",id:"example-5",level:3},{value:"EXAMPLE 6",id:"example-6",level:3},{value:"EXAMPLE 7",id:"example-7",level:3},{value:"EXAMPLE 8",id:"example-8",level:3},{value:"EXAMPLE 9",id:"example-9",level:3},{value:"PARAMETERS",id:"parameters",level:2},{value:"-CommandName",id:"-commandname",level:3},{value:"-MockWith",id:"-mockwith",level:3},{value:"-ModuleName",id:"-modulename",level:3},{value:"-ParameterFilter",id:"-parameterfilter",level:3},{value:"-RemoveParameterType",id:"-removeparametertype",level:3},{value:"-RemoveParameterValidation",id:"-removeparametervalidation",level:3},{value:"-Verifiable",id:"-verifiable",level:3},{value:"CommonParameters",id:"commonparameters",level:3},{value:"INPUTS",id:"inputs",level:2},{value:"OUTPUTS",id:"outputs",level:2},{value:"NOTES",id:"notes",level:2},{value:"RELATED LINKS",id:"related-links",level:2},{value:"VERSION",id:"version",level:2}];function d(e){let n={a:"a",admonition:"admonition",code:"code",em:"em",h2:"h2",h3:"h3",li:"li",p:"p",pre:"pre",ul:"ul",...(0,i.R)(),...e.components};return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsx)(n.admonition,{title:"This page was generated",type:"info",children:(0,t.jsxs)(n.p,{children:["Contributions are welcome in ",(0,t.jsx)(n.a,{href:"https://github.com/pester/Pester/tree/rel/5.x.x",children:"Pester v5-repo"}),"."]})}),"\n",(0,t.jsx)(n.h2,{id:"synopsis",children:"SYNOPSIS"}),"\n",(0,t.jsx)(n.p,{children:"Mocks the behavior of an existing command with an alternate\nimplementation."}),"\n",(0,t.jsx)(n.h2,{id:"syntax",children:"SYNTAX"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-powershell",children:"Mock [[-CommandName] <string>] [[-MockWith] <scriptblock>] [[-ParameterFilter] <scriptblock>]\n [[-ModuleName] <string>] [[-RemoveParameterType] <string[]>]\n [[-RemoveParameterValidation] <string[]>] [-Verifiable]\n"})}),"\n",(0,t.jsx)(n.h2,{id:"description",children:"DESCRIPTION"}),"\n",(0,t.jsx)(n.p,{children:"This creates new behavior for any existing command within the scope of a\nDescribe or Context block.\nThe function allows you to specify a script block\nthat will become the command's new behavior."}),"\n",(0,t.jsx)(n.p,{children:"Optionally, you may create a Parameter Filter which will examine the\nparameters passed to the mocked command and will invoke the mocked\nbehavior only if the values of the parameter values pass the filter.\nIf\nthey do not, the original command implementation will be invoked instead\nof a mock."}),"\n",(0,t.jsx)(n.p,{children:'You may create multiple mocks for the same command, each using a different\nParameterFilter.\nParameterFilters will be evaluated in reverse order of\ntheir creation.\nThe last one created will be the first to be evaluated.\nThe mock of the first filter to pass will be used.\nThe exception to this\nrule are Mocks with 
1no filters.\nThey will always be evaluated last since\nthey will act as a "catch all" mock.'}),"\n",(0,t.jsx)(n.p,{children:"Mocks can be marked Verifiable.\nIf so, the Should -InvokeVerifiable command\ncan be used to check if all Verifiable mocks were actually called.\nIf any\nverifiable mock is not called, Should -InvokeVerifiable will throw an\nexception and indicate all mocks not called."}),"\n",(0,t.jsx)(n.p,{children:"If you wish to mock commands that are called from inside a script or manifest\nmodule, you can do so by using the -ModuleName parameter to the Mock command.\nThis injects the mock into the specified module.\nIf you do not specify a\nmodule name, the mock will be created in the same scope as the test script.\nYou may mock the same command multiple times, in different scopes, as needed.\nEach module's mock maintains a separate call history and verified status."}),"\n",(0,t.jsx)(n.h2,{id:"examples",children:"EXAMPLES"}),"\n",(0,t.jsx)(n.h3,{id:"example-1",children:"EXAMPLE 1"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-powershell",children:'Mock Get-ChildItem { return @{FullName = "A_File.TXT"} }\n'})}),"\n",(0,t.jsx)(n.p,{children:'Using this Mock, all calls to Get-ChildItem will return a hashtable with a FullName property returning "A_File.TXT"'}),"\n",(0,t.jsx)(n.h3,{id:"example-2",children:"EXAMPLE 2"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-powershell",children:'Mock Get-ChildItem { return @{FullName = "A_File.TXT"} } -ParameterFilter { $Path -and $Path.StartsWith($env:temp) }\n'})}),"\n",(0,t.jsx)(n.p,{children:"This Mock will only be applied to Get-ChildItem calls within the user's temp directory."}),"\n",(0,t.jsx)(n.h3,{id:"example-3",children:"EXAMPLE 3"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-powershell",children:'Mock Set-Content {} -Verifiable -ParameterFilter { $Path -eq "some_path" -and $Value -eq "Expected Value" }\n'})}),"\n",(0,t.jsx)(n.p,{children:"When this mock is used, if the Mock is never invoked and Should -InvokeVerifiable is called, an exception will be thrown.\nThe command behavior will do nothing since the ScriptBlock is empty."}),"\n",(0,t.jsx)(n.h3,{id:"example-4",children:"EXAMPLE 4"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-powershell",children:'Mock Get-ChildItem { return @{FullName = "A_File.TXT"} } -ParameterFilter { $Path -and $Path.StartsWith($env:temp\\1) }\nMock Get-ChildItem { return @{FullName = "B_File.TXT"} } -ParameterFilter { $Path -and $Path.StartsWith($env:temp\\2) }\nMock Get-ChildItem { return @{FullName = "C_File.TXT"} } -ParameterFilter { $Path -and $Path.StartsWith($env:temp\\3) }\n'})}),"\n",(0,t.jsx)(n.p,{children:'Multiple mocks of the same command may be used.\nThe parameter filter determines which is invoked.\nHere, if Get-ChildItem is called on the "2" directory of the temp folder, then B_File.txt will be returned.'}),"\n",(0,t.jsx)(n.h3,{id:"example-5",children:"EXAMPLE 5"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-powershell",children:'Mock Get-ChildItem { return @{FullName="B_File.TXT"} } -ParameterFilter { $Path -eq "$env:temp\\me" }\nMock Get-ChildItem { return @{FullName="A_File.TXT"} } -ParameterFilter { $Path -and $Path.StartsWith($env:temp) }\n\nGet-ChildItem $env:temp\\me\n'})}),"\n",(0,t.jsx)(n.p,{children:"Here, both mocks could apply since both filters will pass.\nA_File.TXT will be returned because it was the most recent Mock created."}),"\n",(0,t.jsx)(n.h3,{id:"example-6",children:"EXAMPLE 6"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-powershell",children:'Mock Get-ChildItem { return @{FullName = "B_File.TXT"} } -ParameterFilter { $Path -eq "$env:temp\\me" }\nMock Get-ChildItem { return @{FullName = "A_File.TXT"} }\n\nGet-ChildItem c:\\windows\n'})}),"\n",(0,t.jsx)(n.p,{children:"Here, A_File.TXT will be returned.\nSince no filter was specified, it will apply to any call to Get-ChildItem that does not pass another filter."}),"\n",(0,t.jsx)(n.h3,{id:"example-7",children:"EXAMPLE 7"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-powershell",children:'Mock Get-ChildItem { return @{FullName = "B_File.TXT"} } -ParameterFilter { $Path -eq "$env:temp\\me" }\nMock Get-ChildItem { return @{FullName = "A_File.TXT"} }
1\n\nGet-ChildItem $env:temp\\me\n'})}),"\n",(0,t.jsx)(n.p,{children:"Here, B_File.TXT will be returned.\nEven though the filterless mock was created more recently.\nThis illustrates that filterless Mocks are always evaluated last regardless of their creation order."}),"\n",(0,t.jsx)(n.h3,{id:"example-8",children:"EXAMPLE 8"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-powershell",children:'Mock Get-ChildItem { return @{FullName = "A_File.TXT"} } -ModuleName MyTestModule\n'})}),"\n",(0,t.jsx)(n.p,{children:'Using this Mock, all calls to Get-ChildItem from within the MyTestModule module\nwill return a hashtable with a FullName property returning "A_File.TXT"'}),"\n",(0,t.jsx)(n.h3,{id:"example-9",children:"EXAMPLE 9"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-powershell",children:'Get-Module -Name ModuleMockExample | Remove-Module\nNew-Module -Name ModuleMockExample  -ScriptBlock {\n    function Hidden { "Internal Module Function" }\n    function Exported { Hidden }\n\nExport-ModuleMember -Function Exported\n} | Import-Module -Force\n\nDescribe "ModuleMockExample" {\n    It "Hidden function is not directly accessible outside the module" {\n        { Hidden } | Should -Throw\n    }\n\n    It "Original Hidden function is called" {\n        Exported | Should -Be "Internal Module Function"\n    }\n\n    It "Hidden is replaced with our implementation" {\n        Mock Hidden { "Mocked" } -ModuleName ModuleMockExample\n        Exported | Should -Be "Mocked"\n    }\n}\n'})}),"\n",(0,t.jsx)(n.p,{children:"This example shows how calls to commands made from inside a module can be\nmocked by using the -ModuleName parameter."}),"\n",(0,t.jsx)(n.h2,{id:"parameters",children:"PARAMETERS"}),"\n",(0,t.jsx)(n.h3,{id:"-commandname",children:"-CommandName"}),"\n",(0,t.jsx)(n.p,{children:"The name of the command to be mocked."}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-yaml",children:"Type: System.String\nDefaultValue: ''\nSupportsWildcards: false\nAliases: []\nParameterSets:\n- Name: (All)\n  Position: 0\n  IsRequired: false\n  ValueFromPipeline: false\n  ValueFromPipelineByPropertyName: false\n  ValueFromRemainingArguments: false\nDontShow: false\nAcceptedValues: []\nHelpMessage: ''\n"})}),"\n",(0,t.jsx)(n.h3,{id:"-mockwith",children:"-MockWith"}),"\n",(0,t.jsx)(n.p,{children:"A ScriptBlock specifying the behavior that will be used to mock CommandName.\nThe default is an empty ScriptBlock.\nNOTE: Do not specify param or dynamicparam blocks in this script block.\nThese will be injected automatically based on the signature of the command\nbeing mocked, and the MockWith script block can contain references to the\nmocked commands parameter variables."}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-yaml",children:"Type: System.Management.Automation.ScriptBlock\nDefaultValue: '{}'\nSupportsWildcards: false\nAliases: []\nParameterSets:\n- Name: (All)\n  Position: 1\n  IsRequired: false\n  ValueFromPipeline: false\n  ValueFromPipelineByPropertyName: false\n  ValueFromRemainingArguments: false\nDontShow: false\nAcceptedValues: []\nHelpMessage: ''\n"})}),"\n",(0,t.jsx)(n.h3,{id:"-modulename",children:"-ModuleName"}),"\n",(0,t.jsxs)(n.p,{children:["Optional string specifying the name of the module where this command\nis to be mocked.\nThis should be a module that ",(0,t.jsx)(n.em,{children:"calls"})," the mocked\ncommand; it doesn't necessarily have to be the same module which\noriginally implemented the command."]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-yaml",children:"Type: System.String\nDefaultValue: ''\nSupportsWildcards: false\nAliases: []\nParameterSets:\n- Name: (All)\n  Position: 3\n  IsRequired: false\n  ValueFromPipeline: false\n  ValueFromPipelineByPropertyName: false\n  ValueFromRemainingArguments: false\nDontShow: false\nAcceptedValues: []\nHelpMessage: ''\n"})}),"\n",(0,t.jsx)(n.h3,{id:"-parameterfilter",children:"-ParameterFilter"}),"\n",(0,t.jsx)(n.p,{children:"An optional filter to limit mocking behavior only to usages of\nCommandName where the values of the parameters passed to the command\npass the filter."}),"\n",(0,t.jsx)(n.p,{children:"This ScriptBlock must return a boolean value.\nSee examples for usage."}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-yaml",children:"Type: System.Management.Automation.ScriptBlock\nDefaultValue: ''\nSupportsWildcards: false\nAliases: []\nParameterSets:\n- N
1ame: (All)\n  Position: 2\n  IsRequired: false\n  ValueFromPipeline: false\n  ValueFromPipelineByPropertyName: false\n  ValueFromRemainingArguments: false\nDontShow: false\nAcceptedValues: []\nHelpMessage: ''\n"})}),"\n",(0,t.jsx)(n.h3,{id:"-removeparametertype",children:"-RemoveParameterType"}),"\n",(0,t.jsx)(n.p,{children:"Optional list of parameter names that should use Object as the parameter\ntype instead of the parameter type defined by the function.\nThis relaxes the\ntype requirements and allows some strongly typed functions to be mocked\nmore easily."}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-yaml",children:"Type: System.String[]\nDefaultValue: ''\nSupportsWildcards: false\nAliases: []\nParameterSets:\n- Name: (All)\n  Position: 4\n  IsRequired: false\n  ValueFromPipeline: false\n  ValueFromPipelineByPropertyName: false\n  ValueFromRemainingArguments: false\nDontShow: false\nAcceptedValues: []\nHelpMessage: ''\n"})}),"\n",(0,t.jsx)(n.h3,{id:"-removeparametervalidation",children:"-RemoveParameterValidation"}),"\n",(0,t.jsx)(n.p,{children:"Optional list of parameter names in the original command\nthat should not have any validation rules applied.\nThis relaxes the\nvalidation requirements, and allows functions that are strict about their\nparameter validation to be mocked more easily."}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-yaml",children:"Type: System.String[]\nDefaultValue: ''\nSupportsWildcards: false\nAliases: []\nParameterSets:\n- Name: (All)\n  Position: 5\n  IsRequired: false\n  ValueFromPipeline: false\n  ValueFromPipelineByPropertyName: false\n  ValueFromRemainingArguments: false\nDontShow: false\nAcceptedValues: []\nHelpMessage: ''\n"})}),"\n",(0,t.jsx)(n.h3,{id:"-verifiable",children:"-Verifiable"}),"\n",(0,t.jsx)(n.p,{children:"When this is set, the mock will be checked when Should -InvokeVerifiable is\ncalled."}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-yaml",children:"Type: System.Management.Automation.SwitchParameter\nDefaultValue: False\nSupportsWildcards: false\nAliases: []\nParameterSets:\n- Name: (All)\n  Position: Named\n  IsRequired: false\n  ValueFromPipeline: false\n  ValueFromPipelineByPropertyName: false\n  ValueFromRemainingArguments: false\nDontShow: false\nAcceptedValues: []\nHelpMessage: ''\n"})}),"\n",(0,t.jsx)(n.h3,{id:"commonparameters",children:"CommonParameters"}),"\n",(0,t.jsxs)(n.p,{children:["This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable,\n-InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable,\n-ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see\n",(0,t.jsx)(n.a,{href:"https://go.microsoft.com/fwlink/?LinkID=113216",children:"about_CommonParameters"}),"."]}),"\n",(0,t.jsx)(n.h2,{id:"inputs",children:"INPUTS"}),"\n",(0,t.jsx)(n.h2,{id:"outputs",children:"OUTPUTS"}),"\n",(0,t.jsx)(n.h2,{id:"notes",children:"NOTES"}),"\n",(0,t.jsx)(n.h2,{id:"related-links",children:"RELATED LINKS"}),"\n",(0,t.jsxs)(n.ul,{children:["\n",(0,t.jsx)(n.li,{children:(0,t.jsx)(n.a,{href:"https://pester.dev/docs/v5/commands/Mock",children:"https://pester.dev/docs/v5/commands/Mock"})}),"\n",(0,t.jsx)(n.li,{children:(0,t.jsx)(n.a,{href:"https://pester.dev/docs/v5/usage/mocking",children:"https://pester.dev/docs/v5/usage/mocking"})}),"\n"]}),"\n",(0,t.jsx)(n.h2,{id:"version",children:"VERSION"}),"\n",(0,t.jsx)(n.p,{children:(0,t.jsxs)(n.em,{children:["This page was generated using comment-based help in ",(0,t.jsx)(n.a,{href:"https://github.com/pester/Pester/tree/rel/5.x.x",children:"Pester 5.9.1"}),"."]})})]})}function c(e={}){let{wrapper:n}={...(0,i.R)(),...e.components};return n?(0,t.jsx)(n,{...e,children:(0,t.jsx)(d,{...e})}):d(e)}},7008(e,n,l){l.d(n,{R:()=>r,x:()=>s});var a=l(1763);let t={},i=a.createContext(t);function r(e){let n=a.useContext(i);return a.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function s(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(t):e.components||t:r(e.components),a.createElement(i.Provider,{value:n},e.children)}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.