我正在尝试使用Powershell来模拟模块中的Join-Path。这个模拟将返回一个TestDrive位置,但我一直得到$null而不是TestDrive位置。在我的示例中,模块$OutputPath返回null。我的Mock做错了什么?
foo.psm1
function foobar {
$OutputPath = Join-Path -Path $PSScriptRoot -ChildPath '..\..\..\Output\'
if (!(test-path $OutputPath) ) {
$null = New-Item -ItemType directory -Path $OutputPath
}
}foo.Tests.ps1
import-module foo.psm1
Describe "Mock Example" {
$TestLocation = New-Item -Path "TestDrive:\Output\" -ItemType Directory
Mock -CommandName 'Join-Path' -MockWith { return $TestLocation.FullName } -ModuleName 'Foo' -ParameterFilter {$ChildPath -eq '..\..\..\Output\'}
}发布于 2018-10-07 17:01:55
在我看来,您的代码运行得很好。当函数中的$OutputPath值被设置为TestDrive路径时,我使用了一个Write-Host来检查它的值是什么。我还使用了Assert-MockCalled来验证你的模拟正在被调用:
function foobar {
$OutputPath = Join-Path -Path $PSScriptRoot -ChildPath '..\..\..\Output\'
Write-Host $OutputPath
if (!(test-path $OutputPath) ) {
$null = New-Item -ItemType directory -Path $OutputPath
}
}
Describe "Mock Example" {
$TestLocation = New-Item -Path "TestDrive:\Output\" -ItemType Directory
Mock -CommandName 'Join-Path' -MockWith { $TestLocation } -ParameterFilter {$ChildPath -eq '..\..\..\Output\'}
It 'Should work' {
foobar | should -be $null
Assert-MockCalled Join-Path
}
}您的代码按照设计返回$null。
https://stackoverflow.com/questions/52681908
复制相似问题