Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to Mock New-SCPhysicalComputerConfig #1496

Closed
sasler opened this issue Apr 14, 2020 · 8 comments · Fixed by #2191
Closed

Unable to Mock New-SCPhysicalComputerConfig #1496

sasler opened this issue Apr 14, 2020 · 8 comments · Fixed by #2191

Comments

@sasler
Copy link

sasler commented Apr 14, 2020

1. General summary of the issue

When trying to mock New-SCPhysicalComputerConfig I keep throwing the following error:

"Cannot find an overload for ".ctor" and the argument count: "0"."

I've even tried to use real data on the parameters, to the point where the command works correctly and the object is created in SCVMM when not mocking it. But mocking with this real data I keep getting the above error.

2. Describe Your Environment

Pester version : 4.10.1 C:\Program Files\WindowsPowerShell\Modules\Pester\4.10.1\Pester.psd1
PowerShell version : 5.1.14393.3471
OS version : Microsoft Windows NT 10.0.14393.0

3. Expected Behavior

Mock should work

4.Current Behavior

Throws the following error:

"Cannot find an overload for ".ctor" and the argument count: "0"."

@nohwnd
Copy link
Member

nohwnd commented Apr 15, 2020

Thanks for reporting. Could you share the callstack of the error please? I don't have that cmdlet.

@sasler
Copy link
Author

sasler commented Apr 15, 2020

I think this is it:

at New-SIHVVMHost, C:\Scripts\Test\SI-Deployment-Modules\SIServerHyperV\SIServerHyperVVMHost.psm1: line 454
at <ScriptBlock>, C:\Scripts\Test\SI-Deployment-Modules\SIServerHyperV\SIServerHyperV.Tests.ps1: line 920
at DescribeImpl, C:\Program Files\WindowsPowerShell\Modules\Pester\4.10.1\Functions\Describe.ps1: line 213
at Describe, C:\Program Files\WindowsPowerShell\Modules\Pester\4.10.1\Functions\Describe.ps1: line 105
at <ScriptBlock>, C:\Scripts\Test\SI-Deployment-Modules\SIServerHyperV\SIServerHyperV.Tests.ps1: line 886
at InModuleScope, C:\Program Files\WindowsPowerShell\Modules\Pester\4.10.1\Functions\InModuleScope.ps1: line 84
at <ScriptBlock>, C:\Scripts\Test\SI-Deployment-Modules\SIServerHyperV\SIServerHyperV.Tests.ps1: line 708
at <ScriptBlock>, C:\Program Files\WindowsPowerShell\Modules\Pester\4.10.1\Pester.psm1: line 1111
at Invoke-Pester<End>, C:\Program Files\WindowsPowerShell\Modules\Pester\4.10.1\Pester.psm1: line 1137
at <ScriptBlock>, C:\Users\username\.vscode\extensions\ms-vscode.powershell-preview-2020.4.2\InvokePesterStub.ps1: line 128
at <ScriptBlock>, <No file>: line 1

@nohwnd
Copy link
Member

nohwnd commented Apr 16, 2020

Hmm, okay which module is that coming from? Is that your module? Is it public somewhere so I can replicate?

@sasler
Copy link
Author

sasler commented Apr 16, 2020

It's from VirtualMachineManager module. We are using SCVMM 2016. The cmdlet is New-SCPhysicalComputerConfig.

And this what I'm trying to do:

Mock -CommandName New-SCPhysicalComputerConfig -MockWith { 
   New-MockObject -Type Microsoft.SystemCenter.VirtualMachineManager.PhysicalComputerConfig
}

And module I'm testing it's running the cmdlet with the following parameters:

$vmHostConfig = New-SCPhysicalComputerConfig -BMCAddress $iLONetwork.IPv4Address -BMCPort 623 -BMCProtocol IPMI -BMCRunAsAccount $bmcRunAsAccount `
            -BypassADMachineAccountCheck -ComputerName $Server.Name -Description $Server.Description -SMBiosGuid $computerInfo.SMBiosGUID `
            -VMHostGroup $hostGroup -PhysicalComputerProfile $physicalComputerProfile -PhysicalComputerNetworkAdapterConfig $networkAdapter `
            -BootDiskVolume $global:HyperVData.DiskConfiguration.BootDiskVolume -ErrorAction Stop

I hope this helps and thanks for you help.

@jazzdelightsme
Copy link

I ran into an error similar to this, when trying to mock the Start-BitsTransfer cmdlet (you should have this one, @nohwnd; it's inbox).

I think the problem is that it is looking for a constructor for an enum used in an attribute on one of the parameters. Or something like that:

MethodException:
Line |
 138 |      [ValidateRange(CurrentUser, LocalMachineEnterprise)]
     |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot find an overload for ".ctor" and the argument count: "0".

My version info:

Pester version     : 5.3.1 C:\Users\danthom\Documents\PowerShell\Modules\Pester\5.3.1\Pester.psm1
PowerShell version : 7.2.1
OS version         : Microsoft Windows NT 10.0.22523.0

I was able to work around it by using Mock -RemoveParameterValidation, like so:

    # Because Start-BitsTransfer is soooo slow, and we don't actually need to exercise it.
    Mock Start-BitsTransfer -RemoveParameterValidation 'CertStoreLocation' {

        Copy-Item $Source[0] $Destination[0]
    }

@nohwnd
Copy link
Member

nohwnd commented Apr 22, 2022

This is caused by the built-in proxy command generator doing it wrong. (We had few similar cases where it generates code that cannot be run, e.g. because it tries to re-define read-only variables.)

In this instance it does not correctly define the enum values in the validation:

$m = New-Object System.Management.Automation.CommandMetaData (Get-Command Start-BitsTransfer)
[System.Management.Automation.ProxyCommand]::Create($m)

....


    [Parameter(ParameterSetName='NewFromParams')]
    [ValidateRange(CurrentUser, LocalMachineEnterprise)]
    [Microsoft.BackgroundIntelligentTransfer.Management.CertStoreLocationValue]
    ${CertStoreLocation},

This fails with the same error:

    function f {
        param (
    [ValidateRange(CurrentUser, LocalMachineEnterprise)]
    [Microsoft.BackgroundIntelligentTransfer.Management.CertStoreLocationValue]
    ${CertStoreLocation}
        )
    }
    f

This passes:

    function f {
        param (
    [ValidateRange([Microsoft.BackgroundIntelligentTransfer.Management.CertStoreLocationValue]::CurrentUser, [Microsoft.BackgroundIntelligentTransfer.Management.CertStoreLocationValue]::LocalMachineEnterprise)]
    [Microsoft.BackgroundIntelligentTransfer.Management.CertStoreLocationValue]
    ${CertStoreLocation}
        )
    }
    f

    function f {
        param (
    [ValidateRange('CurrentUser', 'LocalMachineEnterprise')]
    [Microsoft.BackgroundIntelligentTransfer.Management.CertStoreLocationValue]
    ${CertStoreLocation}
        )
    }
    f

We need someone clever to rewrite the ast when the parameter is enum and the values are not quoted.

@nohwnd
Copy link
Member

nohwnd commented Apr 22, 2022

@jazzdelightsme thanks for the repro.

@fflaten
Copy link
Collaborator

fflaten commented Jun 20, 2022

Thanks. I've reported the issue in the PowerShell-repo

Update: Just to clarify, this will also be fixed in the next Pester-release (probably 5.4.0). See related PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants