我想为使用New-AzureRmDnsRecordSet
的脚本编写单元测试。 New-AzureRmDnsRecordSet
上的DnsRecords
参数验证我传入的Microsoft.Azure.Commands.Dns.DnsRecordBase[]
类型是New-AzureRmDnsRecordConfig
的返回值。事情是,我似乎无法得到任何东西转换为DnsRecordBase类型。Mock New-AzureRmDnsRecordConfig返回值的类型Microsoft.Azure.Commands.Dns.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。这是一个更大的脚本的一部分,我真的只是试图嘲笑这些以测试脚本内的其他东西。
我试着用纠缠的新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
}
}
这是一个非常酷的命令,一个对我来说很新但很不幸它不起作用(或者至少看起来不起作用)这个命令。我更新了错误的帖子。谢谢你的回答! –
将它作为GitHub中pester项目的一个问题值得引起注意,它们相当敏感。 –
类似的问题,但看起来像他们解决它︰https://github.com/pester/Pester/issues/806 –