I am trying to test a Function in an script file that is testing the connection to a PC. I am trying to do that by calling the mock "Test-Connection" from an other Script in an Pester Test.
When I run Temp.Testing.ps1
describe 'Test Error Handling' {
$myDir = Split-Path -parent $PSCommandPath
$testFile = "$myDir\TryToRenameComputer.ps1"
.$testFile
mock 'Test-Connection' { $false }
$pathForLogs = "C:\temp\Logs.txt"
it 'exits if Test-Connection Failed'{
TryToRenameComputer -OldName "OldName"
Assert-MockCalled 'Test-Connection' -Times 1 -Scope It
}
}
TryToRenameComputer.ps1
function TryToRenameComputer {
param([parameter(Mandatory=$true)]
[string]$computerName)
if (!(Test-Connection -ComputerName $computerName -Quiet)) {
exit
}
}
it skips the it-statement and shows me no errors. Only "Describing Test Error Handling".
Expected Result:
Actual Result:
I already tried running a other Function and it worked. Also when I am running multiple it-statements all get skipped when I call the Function in 1 it-statement. I also tried rewriting it so it is no function anymore and it worked.
Aucun commentaire:
Enregistrer un commentaire