Powershell 如果模块存在,则进行纠缠测试。不应抛出特定的ExceptionType
如果很难抓住的话,是我还是纠缠者 在Powershell 如果模块存在,则进行纠缠测试。不应抛出特定的ExceptionType,powershell,unit-testing,pester,Powershell,Unit Testing,Pester,如果很难抓住的话,是我还是纠缠者 在BeforeAllscriptblock中,我想测试本地机器上是否存在ActiveDirectory模块 测试不应抛出异常类型[System.IO.FileNotFoundException] 问题1: Describing Add-Patchgroup [-] Should import module ActiveDirectory 99ms PSInvalidCastException: Cannot convert the "[System.
BeforeAll
scriptblock中,我想测试本地机器上是否存在ActiveDirectory
模块
测试不应抛出异常类型[System.IO.FileNotFoundException]
问题1:
Describing Add-Patchgroup
[-] Should import module ActiveDirectory 99ms
PSInvalidCastException: Cannot convert the "[System.IO.FileNotFoundException]" value of type "System.Management.Auto
mation.ScriptBlock" to type "System.Type".
ArgumentTransformationMetadataException: Cannot convert the "[System.IO.FileNotFoundException]" value of type "Syste
m.Management.Automation.ScriptBlock" to type "System.Type".
ParameterBindingArgumentTransformationException: Cannot process argument transformation on parameter 'ExceptionType'
. Cannot convert the "[System.IO.FileNotFoundException]" value of type "System.Management.Automation.ScriptBlock" to typ
e "System.Type".
正确/最佳的方法是什么
Get-Module Module| Remove-Module -Force
Import-Module Module
Import-Module Pester
InModuleScope Module{
Describe 'Add-Patchgroup' {
BeforeAll {
It 'Should import module ActiveDirectory' {
{Import-Module -Name ActiveDirectory -ErrorAction Stop } | Should -Not -Throw -ExceptionType {[System.IO.FileNotFoundException]}
}
}
}
}
在AD模块不可用的机器上运行时出错:
Describing Add-Patchgroup
[-] Should import module ActiveDirectory 99ms
PSInvalidCastException: Cannot convert the "[System.IO.FileNotFoundException]" value of type "System.Management.Auto
mation.ScriptBlock" to type "System.Type".
ArgumentTransformationMetadataException: Cannot convert the "[System.IO.FileNotFoundException]" value of type "Syste
m.Management.Automation.ScriptBlock" to type "System.Type".
ParameterBindingArgumentTransformationException: Cannot process argument transformation on parameter 'ExceptionType'
. Cannot convert the "[System.IO.FileNotFoundException]" value of type "System.Management.Automation.ScriptBlock" to typ
e "System.Type".
问题2:
Describing Add-Patchgroup
[-] Should import module ActiveDirectory 99ms
PSInvalidCastException: Cannot convert the "[System.IO.FileNotFoundException]" value of type "System.Management.Auto
mation.ScriptBlock" to type "System.Type".
ArgumentTransformationMetadataException: Cannot convert the "[System.IO.FileNotFoundException]" value of type "Syste
m.Management.Automation.ScriptBlock" to type "System.Type".
ParameterBindingArgumentTransformationException: Cannot process argument transformation on parameter 'ExceptionType'
. Cannot convert the "[System.IO.FileNotFoundException]" value of type "System.Management.Automation.ScriptBlock" to typ
e "System.Type".
还考虑有一个名为“代码>复杂模块”的模块,导入非常耗时。因此,我想模拟
导入模块-Name ComplexModule
cmdlet。如何执行此操作?您对是否引发异常的测试需要将异常指定为字符串:
It 'Should import module ActiveDirectory' {
{Import-Module -Name ActiveDirectory -ErrorAction Stop } | Should -Not -Throw -ExceptionType System.IO.FileNotFoundException
}
您可以Mock
您的导入模块
,如下所示:
Mock Import-Module {} -ParameterFilter {$Name -eq 'ComplexModule'}
请注意,在您模拟Pester脚本之后,您可能会在成功/合法地使用导入模块时遇到问题,但只要您在模拟之前使用它,我认为您就可以了
还要注意,这个Mock什么也不做(因此是空的{}
),因此它有效地确保了模块没有被加载。如果您的测试依赖于正在加载的模块,您需要要么不Mock
它,要么找到加载所需命令的方法。修改了我的代码,但仍然没有传递:[-]应该导入模块ActiveDirectory 82ms预期不会引发异常,但会引发异常“未加载指定的模块‘ActiveDirectory’,因为在任何模块目录中都找不到有效的模块文件。”从第11行引发。char:18表示运行测试的位置AD模块不可用。在AD模块不可用的位置运行测试。这是prtscr:(不介意其他测试)因此,您编写了一个测试,检查Import Module ActiveDirectory
是否不会引发文件未找到异常。它确实会引发文件未找到异常,因为它未安装在您的系统上,因此您的测试失败,因此您会收到红色文本。ohhh!脸红