- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要从模块内的 PowerShell 函数“纠缠测试”2 个 Azure cmdlet、Get-AzureNetworkSecurityGroup 和 Set-AzureNetworkSecurityRule,如下所示:
$nsg = Get-AzureNetworkSecurityGroup -Name $NsgName
Set-AzureNetworkSecurityRule -Name $NsgRule.name `
-Type Outbound `
# ... other properties here ...
-NetworkSecurityGroup $nsg |
Format-List -Property Name,Location,Label
参数 $NsgName、$NsgRule 并不那么重要,问题是我在模拟 Set-AzureNetworkSecurityRule 时收到错误,如下所示:
Mock Get-AzureNetworkSecurityGroup { return [PSCustomObject] @{ Name='Any' } }
Mock Set-AzureNetworkSecurityRule
Mock Format-List
错误提示:
[-] Error occurred in Describe block 100ms
PSInvalidCastException: Cannot convert the "@{Name=Any}" value of type "System.Management.Automation.PSCustomObject" to type "Microsoft.WindowsAzure.Commands.ServiceManagement.Network.NetworkSecurityGroup.Model.INetworkSecurityGroup".
ArgumentTransformationMetadataException: Cannot convert the "@{Name=Any}" value of type "System.Management.Automation.PSCustomObject" to type "Microsoft.WindowsAzure.Commands.ServiceManagement.Network.NetworkSecurityGroup.Model.INetworkSecurityGroup".
ParameterBindingArgumentTransformationException: Cannot process argument transformation on parameter 'NetworkSecurityGroup'. Cannot convert the "@{Name=Any}" value of type "System.Management.Automation.PSCustomObject" to type "Microsoft.WindowsAzure.Commands.ServiceManagement.Network.NetworkSecurityGroup.Model.INetworkSecurityGroup".
很清楚发生了什么,问题是我不知道如何模拟 INetworkSecurityGroup 类型的对象。我最初的期望是,如果模拟两个 Azure cmdlet 都不会出现问题。
我还尝试使用 -MockWith 模拟 Set-AzureNetworkSecurityRule:
Mock Set-AzureNetworkSecurityRule -MockWith {@{NetworkSecurityGroup='test-stuff'}}
运气不好。
有人能指出我正确的方向吗?
提前致谢
更新,包含完整的描述声明
第一次尝试
$environmentConfig = (Get-AzureEnvironmentConfig "Staging")
Describe 'Set-NetworkSecurityRuleFromObject' {
$role = ($environmentConfig.roles | Where { $_.role_name -eq 'Web'})
$nsgName = Get-NetworkSecurityGroupName -EnvironmentConfig $environmentConfig -Role $role
$rule = $role.nsg_rules.outbound[0]
Mock Get-AzureNetworkSecurityGroup
Mock Set-AzureNetworkSecurityRule
Set-NetworkSecurityRuleFromObject -NsgName -$nsgName -NsgRule $rule -NsgRuleType "Outbound"
It 'Should call mocked functions at least once' {
Assert-MockCalled Get-AzureNetworkSecurityGroup -Times 1 -Scope Describe
Assert-MockCalled Set-AzureNetworkSecurityRule -Times 1 -Scope Describe
}
}
关联PS模块函数调用:
Get-AzureNetworkSecurityGroup -Name $NsgName |
Set-AzureNetworkSecurityRule -Name $NsgRule.name `
-Type $NsgRuleType `
# More parameter initialization here ...
-Protocol $NsgRule.protocol |
Format-List -Property Name,Location,Label
第二次尝试,PS 函数的另一个实现不起作用:
$nsg = Get-AzureNetworkSecurityGroup -Name $NsgName
$nsg |
Set-AzureNetworkSecurityRule -Name $NsgRule.name `
-Type $NsgRuleType `
# More parameter initialization here ...
-Protocol $NsgRule.protocol |
Format-List -Property Name,Location,Label
第三次尝试
Describe 'Set-NetworkSecurityRuleFromObject' {
[ ... ]
Mock Get-AzureNetworkSecurityGroup { return [PSCustomObject] @{ Name='Any' } }
Mock Set-AzureNetworkSecurityRule
Set-NetworkSecurityRuleFromObject -NsgName -$nsgName -NsgRule $rule -NsgRuleType "Outbound"
It 'Should call mocked functions at least once' {
Assert-MockCalled Get-AzureNetworkSecurityGroup -Times 1 -Scope Describe
Assert-MockCalled Set-AzureNetworkSecurityRule -Times 1 -Scope Describe
}
}
最佳答案
这仍然是 Pester 面临的最具挑战性的事情之一。您需要以某种方式模拟 Get-AzureNetworkSecurityGroup
,使其返回一个有效对象,以便稍后传递给 Set-AzureNetworkSecurityGroup
,并且有时可以弄清楚如何做到这一点很棘手。在这个特定的例子中,情况还不错:
Mock Get-AzureNetworkSecurityGroup {
return New-Object Microsoft.WindowsAzure.Commands.ServiceManagement.Network.NetworkSecurityGroup.Model.SimpleNetworkSecurityGroup('Any', 'someLocation', 'someLabel')
}
由于此函数参数是接口(interface)类型,因此您还可以用 C#(或 PowerShell v5)编写一个快速类来实现该接口(interface);有时,如果实际的类在您创建实例后立即开始执行“操作”,这可能会更理想。但是,这个 SimpleNetworkSecurityGroup
类除了保存数据之外并没有真正执行任何操作,并且按原样使用是安全的。 (使用 ILSpy 验证。)
关于unit-testing - Azure cmdlet 调用上出现 Pester 模拟错误 PSInvalidCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36041017/
我正在通过继承 PSCmdlet 在 C# 中编写 Powershell cmdlet。这是包含二进制和脚本组件的 powershell 模块的一部分。 我希望能够从我的二进制 cmdlet 中调用脚
虽然我确实在 Cmdlet Development Guidelines 中找到了很多有关如何命名 Cmdlet 和函数的信息。我没有找到任何关于函数应该以大写还是小写命名的信息。 这里的约定是什么?
我编写了许多 cmdlet。我目前正在处理的那个我想调用我的其他 cmdlet。我正在尝试这样: //Calling cmdlet protected override void ProcessRec
Powershell新手在这里,因此,如果以下问题很愚蠢,请多多包涵... 我想在Powershell中创建VSS快照,并找到了许多使用WMI函数执行此操作的示例。常见的模式是 # Pattern 1
所以我有一个名为 update-name 的 cmdlet,我无权更改它。 我创建了一个名为 update-name 的函数(与 cmdlet 的名称相同)。如何从同名函数调用 cmdlet? 我尝试
我正在编写一组需要 Oauth2 身份验证的 Powershell Cmdlet。我目前能够到达一个新的浏览器窗口打开并且用户复制访问身份验证代码以传递回 cmdlet 的地步。但是,这意味着我必须有
我试图从另一个自定义 cmdlet 运行我的自定义 PowerShell cmdlet。奇怪的是,我可以从 Windows Powershell 手动运行我的自定义 cmdlet 并且它有效,但是当我
我正在考虑使用 Powershell 脚本在我的 SQL Server 2008 实例上自动执行还原过程,我发现有两种不同的方法: 使用 invoke-sqlcmd cmdlet 加载 Microso
背景 带 SDK 的 PowerShell 3 使用 Framework 4.5 的 C# 实现 我已经使用 PSHostUserInterface 实现了我自己的 PSHost 登录。在 PSHos
背景 我在 Windows 7 上使用 Powershell 2.0。 我正在 Powershell 模块中编写一个 cmdlet(“模块”是 Powershell 2.0 的新功能)。 为了测试 c
我知道Get-LocalGroup cmdlet 位于 Microsoft.PowerShell.LocalAccounts模块。我尝试使用命令导入该模块 Get-Module Microsoft.P
在尝试使我的 PowerShell cmdlet(用 C# 编写,未编写脚本)的文档尽可能完整的过程中,我发现了一个有趣的发现。如果我包含正确的 MAML 帮助文件(例如 MyModule.dll-H
我有一个支持 -WhatIf 的 PowerShell 脚本 cmdlet & -Confirm参数。 它通过调用 $PSCmdlet.ShouldProcess() 来做到这一点。执行更改之前的方法
我正在尝试将 az cmdlet 安装到 Kudu 上以用于我的 Azure Function。我目前正在遵循本指南: How to install a PowerShell module in an
我安装了 PowerShell v2.0,除此之外,还安装了 PSCX。 PSCX 是 PowerShell 社区扩展 (http://pscx.codeplex.com/Wikipage)。 我似乎
我在代码中创建了一些 PowerShell cmdlet documentation .除了一件事,一切正常。如果我对任何文档部分有很长的描述,我会将其分成多行,以便可以轻松地从代码中读取。但是,我注
是否有一个 cmdlet 在输出中舍入数字(所有浮点值)? 当我运行以下命令时: get-vm |select ProvisionedSpaceGB,UsedSpaceGB 我将此输出作为表格:
对派生 Cmdlet 的自定义实现的外观很感兴趣,真正了解涉及的工作量。模拟了以下内容: using System.Management.Automation; namespace EMSEngine
我有一个调用另一个脚本(带参数)的脚本。调用的脚本包含 Install-WindowsFeature cmdlet。当我运行脚本时,被调用的脚本运行,但返回以下错误: Install-WindowsF
我目前正在整理一些 Powershell cmdlet。构建它们很容易,但我不知道我是否以可接受的方式构建它们(可以这么说)。 在将数据传递到 Powershell 管道时,是否应遵循任何准则/最佳实
我是一名优秀的程序员,十分优秀!