I have written a function to write the errors that occur in my functions in a csv file, this function is called in the catch block of my functions. I would like to write a Test in Pester to check that my function works correctly, but to be honest I don't know where to start, I have tried some things but they don't work for me, I have also been reading in the documentation but I am still not clear, I would appreciate any help/comments.
Here is the function for which I want to write a Test in Pester:
function Write-Logs {
param (
[ValidateSet("Error")]$MessageType,
[string][Parameter(Mandatory=$true)]$Message,
$LogFilePath,
$Source
)
$CSVLogPath = Join-Path -Path $PSScriptRoot -ChildPath ".\errorslog.csv"
$CSVLogObject = [PSCustomObject] @{
Date = Get-Date
Message = $Message
MessageType = $MessageType
Source = $Source
}
$CSVLogObject | Export-Csv -Path $CSVLogPath -NoTypeInformation -Encoding UTF8 -Append
}
and so I´m calling the function in the catch block:
catch {
Write-Logs -LogFilePath:$CSVLogPath -Message:$Error[0].Exception.Message `
-Source:"FunctionName()" -MessageType:"Error"
return
}