The*_*mer 2 powershell unit-testing pester
我有一个 PowerShell .ps1 文件,其中包含脚本顶部的函数,后跟调用这些函数的不同命令。我正在使用 Pester 对我的脚本文件进行单元测试。
如何模拟 PowerShell .ps1 脚本中的函数?
我曾尝试模拟该函数,但收到一条错误消息,提示“找不到命令”。
我还尝试在描述块中添加一个空的“虚拟”函数。这不会给我上述错误,但它没有正确地模拟脚本中的函数。
我有两个文件。一个保存测试,另一个保存函数和对函数的调用。下面是两个例子:
Function Backup-Directory([switch]$IsError)
{
If($IsError)
{
Write-Error "Error"
Exit 1
}
}
Backup-Directory $true
Run Code Online (Sandbox Code Playgroud)
$here = (Split-Path -Parent $MyInvocation.MyCommand.Path) -replace '\\test', '\main'
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
$productionFile = "$here\$sut"
Describe "File1" {
Context "When the Back-Directory outputs an error." {
# Arrange
Mock Back-Directory { }
Mock Write-Error
# Act
& $productionFile
$hasSucceeded = $?
# Assert
It "Calls Backup-Directory" {
Assert-MockCalled Backup-Directory -Exactly 1 -ParameterFilter {
$IsError -eq $true
}
}
It "Writes error message." {
Assert-MockCalled Write-Error -Exactly 1 -ParameterFilter {
$Message -eq "Error"
}
}
It "Exits with an error." {
$hasSucceeded | Should be $false
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不认为这是可能的。至少在您当前的实现中。不久前我问了同样的问题...... Pester Issue 414
但是您可以将该内部函数拆分为同一目录中的另一个脚本文件,从而允许您对其进行单元测试和模拟。您只需要在主脚本文件中添加 dot source 函数即可使用它:
主函数.ps1:
# Main script
function Main-Function {
# if debugging, set moduleRoot to current directory
if ($MyInvocation.MyCommand.Path) {
$moduleRoot = Split-Path -Path $MyInvocation.MyCommand.Path
}else {
$moduleRoot = $PWD.Path
}
# dot source the inner function
. "$moduleRoot\Inner-Function.ps1"
Write-Output "This is the main script. Calling the inner function"
Inner-Function
Write-Output "Back in the main script"
}
Run Code Online (Sandbox Code Playgroud)
内部功能.ps1:
function Inner-Function {
Write-Output "This is the inner function"
}
Run Code Online (Sandbox Code Playgroud)
Main-Function.Tests.ps1:
$moduleRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
# Load Testing Function
. "$moduleRoot\Main-Function.ps1"
# Load Supporting Functions
. "$moduleRoot\Inner-Function.ps1"
Describe 'Main Script' {
Mock Inner-Function { return "This is a mocked function" }
It 'uses the mocked function' {
(Main-Function -match "This is a mocked function") | Should Be $true
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个非常好的方法,因为我们可以对内部函数进行单元测试,并且随着逻辑的增长,向其中添加测试非常容易(并且可以独立于其余脚本/函数完成)。
内部功能.Tests.ps1:
$moduleRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
# Load Testing Function
. "$moduleRoot\Inner-Function.ps1"
Describe 'Inner Function' {
It 'outputs some text' {
Inner-Function | Should Be "This is the inner function"
}
}
Run Code Online (Sandbox Code Playgroud)
这里有两个要点……
无论您当前的执行目录如何,都可以查找相关函数位置...
if ($MyInvocation.MyCommand.Path) {
$moduleRoot = Split-Path -Path $MyInvocation.MyCommand.Path
}else {
$moduleRoot = $PWD.Path
}
Run Code Online (Sandbox Code Playgroud)
和
Dot Sourcing 依赖于主函数中的函数以及单元测试文件...... . "$moduleRoot\Inner-Function.ps1"
Split-Path -Path $MyInvocation.MyCommand.Path
是$null
在用户会话中,但是将不会被$null
从执行上下文中调用时。意思是,您可以进入C:\users\nhudacin
并仍然正确加载此脚本/模块。否则,您必须始终在与它所在位置相同的目录中执行此脚本/模块,而不使用$MyInvoation
变量。
归档时间: |
|
查看次数: |
1546 次 |
最近记录: |