@@ -228,17 +228,24 @@ Describe "ContainerNetworkTools.psm1" {
228228
229229 It " Should use HNS module if HostNetworkingService is not installed" {
230230 Mock Get-Module - ModuleName ' ContainerNetworkTools' - ParameterFilter { $Name -eq ' HostNetworkingService' }
231- Mock Get-Module - ModuleName ' ContainerNetworkTools' - ParameterFilter { $Name -eq ' HNS' } - MockWith { return @ {} }
231+ Mock Get-Module - ModuleName ' ContainerNetworkTools' - ParameterFilter { $Name -eq ' HNS' } - MockWith {
232+ return @ {Name = ' HNS' ; Path = " TestDrive:\PowerShell\Modules\HNS\HNS.psm1" }
233+ }
232234
233235 Initialize-NatNetwork - Force
234236
235237 Should - Invoke Import-Module - Times 0 - Scope It - ModuleName ' ContainerNetworkTools' - ParameterFilter { $Name -eq ' HostNetworkingService' }
236- Should - Invoke Import-Module - Times 1 - Scope It - ModuleName ' ContainerNetworkTools' - ParameterFilter { $Name -eq ' HNS' }
238+ Should - Invoke Get-Module - Times 1 - Scope It - ModuleName ' ContainerNetworkTools' - ParameterFilter { $Name -eq ' HNS' }
239+
240+ # NOTE: Because we are piping the HNS module info from Get-Module, Pester's Should -Invoke
241+ # often struggle to intercept commands invoked via the pipeline, especially when parameter
242+ # binding happens implicitly or when the function relies on parameter value from a piped object.
243+ Should - Invoke Import-Module - Times 1 - Scope It - ModuleName ' ContainerNetworkTools'
237244 }
238245
239246 It " Should throw an error when importing HNS module fails" {
240247 Mock Get-Module - ModuleName ' ContainerNetworkTools' - ParameterFilter { $Name -eq ' HostNetworkingService' }
241- Mock Get-Module - ModuleName ' ContainerNetworkTools' - ParameterFilter { $Name -eq ' HNS' } - MockWith { return @ {} }
248+ Mock Get-Module - ModuleName ' ContainerNetworkTools' - ParameterFilter { $Name -eq ' HNS' } - MockWith { return @ { Name = ' HNS ' } }
242249 Mock Import-Module - ModuleName ' ContainerNetworkTools' - MockWith { Throw ' Error message.' }
243250
244251 { Initialize-NatNetwork - Force } | Should - Throw " Could not import HNS module. Error message."
0 commit comments