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

时间:2017-08-24 15:55:04

标签: powershell azure pester

我正在尝试为使用New-AzureRmDnsRecordSet的脚本编写单元测试。 DnsRecords上的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

我已经尝试了一切代替&#34; 1.2.3.4&#34; ...整数,字符串,Hashtable,数组,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
  }
}

2 个答案:

答案 0 :(得分:1)

模拟此对象的方法是:

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

我遇到了同样的问题,发现了完整的类型 {-3}}查看为-RecordSet参数列出的类型。

更新:如果您可以使用$object.GetType().FullName获取对象类型的实例,则可以更轻松地获取完整类型。例如:

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

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

答案 1 :(得分:0)

您可以使用New-MockObject模拟所需的对象类型:https://github.com/pester/Pester/wiki/New-MockObject

  

New-MockObject是一个Pester函数(在Pester 3.4.4中引入)   允许你创建&#34;假&#34;几乎任何类型的对象都可以运行   Pester嘲笑。这些&#34;虚假物品&#34;让你的模拟返回相同的   键入作为模拟将结果传递给实体的函数   这是强类型的。