I am trying to write Pester tests for my Azure automation runbooks. The runbook script uses the Get-AutomationVariable
cmdlet, and I am attempting to mock it via:
mock 'Get-AutomationVariable' {return "localhost:44300"} -ParameterFilter { $name -eq "host"}
which results in the error
CommandNotFoundException: The term 'Get-AutomationVariable' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
The use of the -ModuleName
parameter does not seem appropriate as I am calling the method from a script not a module. Trying to provide a stub module results in the same error being thrown.
. "$here\$sut" "CN=teset, OU=Test" "CN=SubCA02, OU=Test"
Get-Module -Name RunbookMock | Remove-Module
New-Module -Name RunbookMock -ScriptBlock {
function Get-AutomationVariable {
[CmdLetBinding()]
param(
[string]$Name
)
""
}
Export-ModuleMember Get-AutomationVariable
} | Import-Module -Force
describe 'Pin-Certificate' {
it 'should add an entry to the pinned certificate list'{
mock 'Get-AutomationVariable' { return "sastoken"} -ParameterFilter { $Name -eq "StorageSasToken"} -
mock 'Get-AutomationVariable' {return "localhost:44300"} -ParameterFilter { $name -eq "host"}
}
}
Per the comments, your code should work. In the past I have just declared an empty function rather than a full module. E.g:
function MyScript {
Get-FakeFunction
}
Describe 'Some Tests' {
function Get-Fakefunction {}
Mock 'Get-Fakefunction' { write-output 'someresult' }
$Result = MyScript
it 'should invoke Get-FakeFunction'{
Assert-MockCalled 'Get-Fakefunction' -Times 1
}
it 'should return someresult'{
$Result | Should -Be 'someresult'
}
}