我正在尝试为使用New-AzureRmDnsRecordSet
. DnsRecords
on 参数New-AzureRmDnsRecordSet
验证我传入的类型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
以获取真实对象,因为该命令行开关需要我运行 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
}
}