title: Pester mock error PSInvalidCastException on Azure cmdlet call
body: <p>I need to "Pester-test" 2 Azure cmdlets, <strong>Get-AzureNetworkSecurityGroup</strong> and <strong>Set-AzureNetworkSecurityRule</strong> from a PowerShell function inside a module, that looks like following:</p>
  
  <pre><code>  $nsg = Get-AzureNetworkSecurityGroup -Name $NsgName
    Set-AzureNetworkSecurityRule -Name $NsgRule.name `
        -Type Outbound `
        # ... other properties here ... 
        -NetworkSecurityGroup $nsg |
      Format-List -Property Name,Location,Label
  </code></pre>
  
  <p>The parameters $NsgName, $NsgRule are not so important, the problem is I receive errors when mocking Set-AzureNetworkSecurityRule like:</p>
  
  <pre><code>Mock Get-AzureNetworkSecurityGroup { return [PSCustomObject] @{ Name='Any' } }
  Mock Set-AzureNetworkSecurityRule
  Mock Format-List
  </code></pre>
  
  <p>The error says:</p>
  
  <pre><code> [-] Error occurred in Describe block 100ms
     PSInvalidCastException: Cannot convert the "@{Name=Any}" value of type "System.Management.Automation.PSCustomObject" to type "Microsoft.WindowsAzure.Commands.ServiceManagement.Network.NetworkSecurityGroup.Model.INetworkSecurityGroup".
     ArgumentTransformationMetadataException: Cannot convert the "@{Name=Any}" value of type "System.Management.Automation.PSCustomObject" to type "Microsoft.WindowsAzure.Commands.ServiceManagement.Network.NetworkSecurityGroup.Model.INetworkSecurityGroup".
     ParameterBindingArgumentTransformationException: Cannot process argument transformation on parameter 'NetworkSecurityGroup'. Cannot convert the "@{Name=Any}" value of type "System.Management.Automation.PSCustomObject" to type "Microsoft.WindowsAzure.Commands.ServiceManagement.Network.NetworkSecurityGroup.Model.INetworkSecurityGroup".
  </code></pre>
  
  <p>It's quite clear what's happening, the issue is I don't know how to mock an object of type <strong>INetworkSecurityGroup</strong>. My expectation was initially to be no problem if mocking both Azure cmdlets.</p>
  
  <p>I've also tried mocking <strong>Set-AzureNetworkSecurityRule</strong> using <strong>-MockWith</strong>:</p>
  
  <pre><code>Mock Set-AzureNetworkSecurityRule -MockWith {@{NetworkSecurityGroup='test-stuff'}}
  </code></pre>
  
  <p>with no luck.</p>
  
  <p>Can anyone point me to the right direction ?<br>
  Thanks in advance  </p>
  
  <p><strong>UPDATE</strong> with full <strong>Describe</strong> statement  </p>
  
  <p>First try</p>
  
  <pre><code>  $environmentConfig = (Get-AzureEnvironmentConfig "Staging")
  
    Describe 'Set-NetworkSecurityRuleFromObject' {
      $role = ($environmentConfig.roles | Where { $_.role_name -eq 'Web'})
      $nsgName = Get-NetworkSecurityGroupName -EnvironmentConfig $environmentConfig -Role $role
      $rule = $role.nsg_rules.outbound[0]
  
      Mock Get-AzureNetworkSecurityGroup
      Mock Set-AzureNetworkSecurityRule
  
      Set-NetworkSecurityRuleFromObject -NsgName -$nsgName -NsgRule $rule -NsgRuleType "Outbound"
  
      It 'Should call mocked functions at least once' {
        Assert-MockCalled Get-AzureNetworkSecurityGroup -Times 1 -Scope Describe
        Assert-MockCalled Set-AzureNetworkSecurityRule -Times 1 -Scope Describe
      }
    }
  </code></pre>
  
  <p>Associated PS module function call:</p>
  
  <pre><code>  Get-AzureNetworkSecurityGroup -Name $NsgName |
      Set-AzureNetworkSecurityRule -Name $NsgRule.name `
        -Type $NsgRuleType `
        # More parameter initialization here ...
        -Protocol $NsgRule.protocol |
      Format-List -Property Name,Location,Label
  </code></pre>
  
  <p>Second try, another implementation of PS function that didn't work:</p>
  
  <pre><code>  $nsg = Get-AzureNetworkSecurityGroup -Name $NsgName
    $nsg |
      Set-AzureNetworkSecurityRule -Name $NsgRule.name `
        -Type $NsgRuleType `
        # More parameter initialization here ...
        -Protocol $NsgRule.protocol |
      Format-List -Property Name,Location,Label
  </code></pre>
  
  <p>Third try</p>
  
  <pre><code>  Describe 'Set-NetworkSecurityRuleFromObject' {
      [ ... ]
      Mock Get-AzureNetworkSecurityGroup { return [PSCustomObject] @{ Name='Any' } }
      Mock Set-AzureNetworkSecurityRule
  
      Set-NetworkSecurityRuleFromObject -NsgName -$nsgName -NsgRule $rule -NsgRuleType "Outbound"
  
      It 'Should call mocked functions at least once' {
        Assert-MockCalled Get-AzureNetworkSecurityGroup -Times 1 -Scope Describe
        Assert-MockCalled Set-AzureNetworkSecurityRule -Times 1 -Scope Describe
      }
    }
  </code></pre>