Powershell Pester单元测试无法模拟扩展存档
我遇到了一个问题,我试图在pester单元测试中模拟Windows内置cmdletPowershell Pester单元测试无法模拟扩展存档,powershell,unit-testing,pester,Powershell,Unit Testing,Pester,我遇到了一个问题,我试图在pester单元测试中模拟Windows内置cmdletExpand Archive。不知何故,在执行过程中会执行原始cmdlet,而不是模拟的变体。我曾尝试将扩展归档模拟放在描述范围之外,但没有成功 以下是我想测试的模块: class Validator { [bool] Validate([string] $filePath) { return $False } } class DocxValidator : Valid
Expand Archive
。不知何故,在执行过程中会执行原始cmdlet,而不是模拟的变体。我曾尝试将扩展归档
模拟放在描述
范围之外,但没有成功
以下是我想测试的模块:
class Validator {
[bool] Validate([string] $filePath) {
return $False
}
}
class DocxValidator : Validator {
[bool] Validate([string] $filePath) {
try {
$tempFile = [System.IO.Path]::GetTempFileName() + ".zip"
Copy-Item $filePath -Destination $tempFile
$tempDirectory = ([System.IO.Path]::GetTempPath()).ToString() + "validator"
Expand-Archive -LiteralPath $tempFile -DestinationPath $tempDirectory -Force
Remove-Item -Path $tempDirectory -Recurse -Force
return $true
}
catch {
Write-Host "Failed to validate file $($filePath)"
}
return $false
}
}
function Get-Validator {
[OutputType([Validator])]
param([string] $ValidatorType)
switch ($ValidatorType) {
"xml" {
return [XmlValidator]::new()
}
"json" {
return [JsonValidator]::new()
}
"docx" {
return [DocxValidator]::new()
}
Default {
throw "invalid validator type"
}
}
}
Export-ModuleMember -Function Get-Validator
这是单元测试:
Get-Module Validation-Helper | Remove-Module -Force
$modulePath = Resolve-Path -Path "$($PSScriptRoot)\..\Validation-Helper\Validation-Helper.psm1"
Import-Module $modulePath
InModuleScope Validation-Helper {
Describe 'Validator for docx' {
Context 'when the document is valid' {
# arrange
BeforeAll {
Mock Write-Host { }
Mock Copy-Item { }
# this was another hint, to explicitly add the module scope here
Mock -ModuleName Validation-Helper Expand-Archive { } -Verifiable -ParameterFilter {
}
}
$tempFile = [System.IO.Path]::GetTempFileName()
$docxValidator = Get-Validator -ValidatorType "docx"
# act | assert
It 'returns true' {
$docxValidator.Validate($tempFile) | Should Be $true
}
}
}
}
我在Validate()方法的catch分支中得到的异常:
背景:
我目前正在开发Powershell模块,该模块将验证Word文档docx是否已损坏。其思想是,每个docx文档基本上都是一个zip文件。因此,应该可以使用任何提取方法来提取它-如果提取过程中没有发生错误,则文件正常。尝试从
的Mock
中删除-ParameterFilter{}
部分。我怀疑,如果使用cmdlet时没有任何参数,而您使用cmdlet时有参数,那么它只能调用Mock。不,不幸的是,这并不能解决问题。我仍然有相同的异常。请尝试从展开存档
的模拟
中删除-ParameterFilter{}
部分。我怀疑,如果使用cmdlet时没有任何参数,而您使用cmdlet时有参数,那么它只能调用Mock。不,不幸的是,这并不能解决问题。我仍然有同样的例外。
System.Management.Automation.MethodInvocationException: Exception calling ".ctor" with "3" argument(s): "Central Directory corrupt." ---> System.IO.InvalidDataException: Central Directory corrupt. ---> System.IO.IOException: An attempt was made to move the file pointer before the beginning of the file.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.SeekCore(Int64 offset, SeekOrigin origin)
at System.IO.FileStream.Seek(Int64 offset, SeekOrigin origin)
at System.IO.Compression.ZipArchive.ReadEndOfCentralDirectory()
--- End of inner exception stack trace ---
at System.IO.Compression.ZipArchive.ReadEndOfCentralDirectory()
at System.IO.Compression.ZipArchive.Init(Stream stream, ZipArchiveMode mode, Boolean leaveOpen)
at System.IO.Compression.ZipArchive..ctor(Stream stream, ZipArchiveMode mode, Boolean leaveOpen)
--- End of inner exception stack trace ---
at System.Management.Automation.DotNetAdapter.AuxiliaryConstructorInvoke(MethodInformation methodInformation, Object[] arguments, Object[] originalArguments)
at System.Management.Automation.DotNetAdapter.ConstructorInvokeDotNet(Type type, ConstructorInfo[] constructors, Object[] arguments)
at Microsoft.PowerShell.Commands.NewObjectCommand.CallConstructor(Type type, ConstructorInfo[] constructors, Object[] args)