1

我正在尝试为使用New-AzureRmDnsRecordSet. DnsRecordson 参数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
  }
}
4

2 回答 2

1

模拟这个对象的方法是:

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

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

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

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

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

于 2018-03-13T17:14:29.117 回答
0

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

New-MockObject 是一个 Pester 函数(在 Pester 3.4.4 中引入),它允许您创建几乎任何类型的“假”对象以在 Pester 模拟中运行。这些“假对象”允许您的模拟返回与其模拟的函数相同的类型,以将结果传递给强类型的实体。

于 2017-08-24T16:08:49.683 回答