gpt4 book ai didi

powershell - 模拟 New-AzureRmDnsRecordConfig 返回 Microsoft.Azure.Commands.Dns.DnsRecordBase[] 类型的值

转载 作者:行者123 更新时间:2023-12-03 03:04:28 24 4
gpt4 key购买 nike

我正在尝试为使用 New-AzureRmDnsRecordSet 的脚本编写单元测试。 New-AzureRmDnsRecordSet 上的 DnsRecords 参数验证我是否传入了 Microsoft.Azure.Commands.Dns.DnsRecordBase[] 类型,该类型是New-AzureRmDnsRecordConfig 的返回值。问题是,我似乎无法将任何内容转换为 DnsRecordBase 类型。

这是一个显示我的问题的虚拟脚本:

function test-mocking {
$arecordtype = New-AzureRmDnsRecordConfig -Ipv4Address 1.2.3.4
New-AzureRmDnsRecordSet -Name "UT" -RecordType A -ResourceGroupName 'RG-UT' -TTL 60 -ZoneName 'zone1' -DnsRecords $arecordtype -Confirm:$False -Overwrite
}

Describe 'test-mocking' {
Mock New-AzureRmDnsRecordSet { return 'sup' }
Mock New-AzureRmDnsRecordConfig { return '1.2.3.4' }

it 'does nothing' {
test-mocking
Assert-MockCalled New-AzureRmDnsRecordSet
}
}

输出:

Describing test-mocking
[-] does nothing 47ms
PSInvalidCastException: Cannot convert the "1.2.3.4" value of type "System.String" to type "Microsoft.Azure.Commands.Dns.DnsRecordBase[]".
ArgumentTransformationMetadataException: Cannot convert the "1.2.3.4" value of type "System.String" to type "Microsoft.Azure.Commands.Dns.DnsRecordBase[]".
ParameterBindingArgumentTransformationException: Cannot process argument transformation on parameter 'DnsRecords'. Cannot convert the "1.2.3.4" value of type "System.String" to type "Microsoft.Azure.Commands.Dns.DnsRecordBase[]".
at test-mocking, C:\Temp\testMock.ps1: line 3
at <ScriptBlock>, C:\Temp\testMock.ps1: line 11

我已经尝试了所有代替“1.2.3.4”的方法...整数、字符串、哈希表、数组、System.Object、$null

我也不能只运行 New-AzureRmDnsRecordConfig 来获取真实对象,因为该 commandlet 要求我运行 Login-AzureRmAccount。这是一个更大的脚本的一部分,我真的只是想模拟这些来测试脚本内的其他内容。

我尝试使用 Pester 的新 CMDLET New-MockObject 但收到此错误:

[-] does nothing 110ms
MemberAccessException: Cannot create an abstract class.
MethodInvocationException: Exception calling "GetUninitializedObject" with "1" argument(s): "Cannot create an abstract class."
at New-MockObject, C:\Program Files\WindowsPowerShell\Modules\Pester\4.0.6\Functions\New-MockObject.ps1: line 22
at <ScriptBlock>, <No file>: line 1
at <ScriptBlock>, C:\Program Files\WindowsPowerShell\Modules\Pester\4.0.6\Functions\Mock.ps1: line 1111
at ExecuteBlock, C:\Program Files\WindowsPowerShell\Modules\Pester\4.0.6\Functions\Mock.ps1: line 1123
at Invoke-Mock, C:\Program Files\WindowsPowerShell\Modules\Pester\4.0.6\Functions\Mock.ps1: line 966
at <ScriptBlock><Process>, <No file>: line 119
at test-mocking, C:\Temp\testMock.ps1: line 2
at <ScriptBlock>, C:\Temp\testMock.ps1: line 11

代码:

function test-mocking {
$arecordtype = New-AzureRmDnsRecordConfig -Ipv4Address 1.2.3.4
New-AzureRmDnsRecordSet -Name "UT" -RecordType A -ResourceGroupName 'RG-UT' -TTL 60 -ZoneName 'zone1' -DnsRecords $arecordtype -Confirm:$False -Overwrite
}

Describe 'test-mocking' {
Mock New-AzureRmDnsRecordSet { return 'sup' }
Mock New-AzureRmDnsRecordConfig { return New-MockObject -Type Microsoft.Azure.Commands.Dns.DnsRecordBase }

it 'does nothing' {
test-mocking
Assert-MockCalled New-AzureRmDnsRecordSet
}
}

最佳答案

模拟这个对象的方法是:

New-MockObject -Type ([Microsoft.Azure.Commands.Dns.DnsRecordSet])

我遇到了同样的问题,并在中找到了完整的类型 Set-AzureRmDnsRecordSet online help通过查看为 -RecordSet 参数列出的类型。

更新:如果您可以使用 $object.GetType().FullName 获取对象类型的实例,则有一种更简单的方法来获取完整类型:

$temp = Get-AzureRmDnsRecordSet -ZoneName "myzone.com" -ResourceGroupName "myResources" -Name "mysite" -RecordType A
$temp.GetType().FullName

这将输出Microsoft.Azure.Commands.Dns.DnsRecordSet

关于powershell - 模拟 New-AzureRmDnsRecordConfig 返回 Microsoft.Azure.Commands.Dns.DnsRecordBase[] 类型的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45866150/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com