I have a PowerShell script that I want to test using Pester. For that I want to mock the Get-ChildItem like that
$expected = [System.Collections.Generic.List[System.IO.FileInfo]]::new()
$expected.Add([System.IO.FileInfo]::new('SmokeTest.txt'))
Now, I want to try a filter based on the CreationTime. I tried to create a file like that
$expected.Add([System.IO.FileInfo]::new({
Name = 'Smoke Test.txt'
CreationTime = [DateTime]::ParseExact('2023-01-01 22:00',
"yyyy-MM-dd HH:mm", $null)
}))
but I get an error
Validate files to delete.validate files with date.should return a list of expected files (mock) 8ms (8ms|1ms) ArgumentException: Illegal characters in path. MethodInvocationException: Exception calling ".ctor" with "1" argument(s): "Illegal characters in path."
I googled but I can't find how to create a System.IO.FileInfo with the CreationTime.
I usually write code in Visual Studio in c# and then convert. The issue with you code is FileInfo new only has one constructor with the filename.