I've found a lot of questions about mocking a function (method) within a PowerShell class. The question I have (and can't find an answer for) is, is it possible to mock a cmdlet call that is contained within a PowerShell class?
To be clear - this is about testing the function/method in the class, not mocking the function/method from the class for which there are a number of options available.
Example: PowerShell Class - test.psm1
class MyClass {
[string]$filecontent
MyClass() {}
[void] GetFileContent([string]$filepath) {
$this.filecontent = Get-Content -Path $filepath -raw
}
}
Pester file:
using module .\test.psm1
Describe "MyClass" {
BeforeAll {
$objClass = [MyClass]::new()
Mock Get-Content { return "this-is-a-test" }
$objClass.GetFileContent("c:\temp\myfile.txt")
}
It "Should update class variable" {
$objClass.filecontent | Should -be "test-is-a-test"
}
AfterAll {
Remove-Variable objClass
}
}
I had expected (or rather hoped) to mock the cmdlet Get-Content within the class function/method GetFileContent, but Get-Content is not mocked - and it tries to call the file which may not exist in testing. Any ideas of how to mock these cmdlets?
Many thanks
Martin
The key to make this work is to use the
-ModuleNameParameter in yourMockstatement, this is so the cmdlet gets overridden in the Module's scope.The argument for
ModuleNamein this case would betest, this is determined by yourpsm1file name.In summary:
Alternatively, hint provided by zett42, you can use
InModuleScopewrappingDescribe: