Reputation: 948
I'm using several Powershell scripts with Advanced Installer 15.1 that I wish to test using Pester. Advanced Installer provides two Cmdlets in order to access MSI variables from Powershell scripts, AI_GetMsiProperty and AI_SetMsiProperty, which I would like to mock in my unit tests. The problem is that standard use of these cmdlets is without specifying parameters, e.g.
AI_GetMsiProperty MYPROPERTY
AI_SetMsiProperty MYPROPERTY "Newvalue"
Pester's Mocking capabilities allow you to use the ParameterFilter
parameter to return multiple values to multiple calls of the same mock, based on the a named parameter:
Mock Get-ChildItem { return @{FullName = "A_File.TXT"} } -ParameterFilter { $Path -and $Path.StartsWith($env:temp\1) }
Mock Get-ChildItem { return @{FullName = "B_File.TXT"} } -ParameterFilter { $Path -and $Path.StartsWith($env:temp\2) }
Mock Get-ChildItem { return @{FullName = "C_File.TXT"} } -ParameterFilter { $Path -and $Path.StartsWith($env:temp\3) }
However, as AI_GetMsiProperty does not use named parameters, I'm not sure how to mock multiple get or set calls based on parameters.
Mock AI_GetMsiProperty { return "value1" } -ParameterFilter { ????? }
Mock AI_GetMsiProperty { return "value2" } -ParameterFilter { ????? }
Any ideas how I can accomplish this?
Upvotes: 1
Views: 291
Reputation: 23395
If these commands are true PowerShell cmdlets you should be able to use Get-Help
with them to determine what the positional parameter names are. However if per the other answer they are not cmdlets then Mock
cannot be used on them directly.
A possible workaround in this instance would be to wrap the commands in a Function
and then Mock
this function instead:
Function Get-MsiProperty ($Property, $Value) {
AI_GetMsiProperty $Property $Value
}
Mock example:
Mock Get-MsiProperty -ParameterFilter { $Property -eq 'somevalue' }
Upvotes: 1
Reputation: 1347
Most likely this cannot be done and this is because from what I know the mentioned get and set msi properties cmdlets are some sort of a pseudo-cmdlets. More exactly they are not PowerShell syntax specific cmdlets, but Advanced Installer syntax specific and they would work only during the install time of the setup package built with Advanced Installer.
If you try to run the related cmdlets outside of the install package built with Advanced Installer, PowerShell will just fail to interpret them.
Upvotes: 1