I'm using Pester, a PowerShell testing library to help with TDD / unit test coverage.
I'm trying to mock out Get-ChildItem for tests I have inside a module that are supposed to do our environment setup. If I have my mocked Get-ChildItem function return a plain string it works fine but if I have it return this array, it doesn't return anything.
Describe "Get-HighestBuildNumber" {
Context "Get-ChildItem mocked to returns 12345, 12346, 12348, Foobar12349" {
$directory1 = New-Object -TypeName System.IO.DirectoryInfo -ArgumentList "12345"
$directory2 = New-Object -TypeName System.IO.DirectoryInfo -ArgumentList "12346"
$directory3 = New-Object -TypeName System.IO.DirectoryInfo -ArgumentList "12348"
$directory4 = New-Object -TypeName System.IO.DirectoryInfo -ArgumentList "Foobar12349"
$fakeListingOfDirectories = @( $directory1, $directory2, $directory3, $directory4 )
Mock -ModuleName EnvironmentSetup Get-ChildItem {
#return "this return text works" #this works
return $fakeListingOfDirectories #this return array does not work
}
it "should return 12348" {
Get-HighestBuildNumber -buildRoot "C:\my\test\directory" | Should Be "12348"
}
}
}
In the code under test, I've set a breakpoint and calling the mocked Get-ChildItem and can tell something is different.
When it's called with the string mock - everything is okay.
When it is called with the array mock - it returns nothing, not even the standard listing of files and directories.. so it looks like the mock is doing something.
I'm trying to figure out why Get-ChildItem isn't returning my array of DirectoryInfo items.
Thanks!
Edit: When I changed:
Mock -ModuleName EnvironmentSetup Get-ChildItem {
return $fakeListingOfDirectories #this return array does not work
}
to return a different literal:
Mock Get-ChildItem -ModuleName NavEnvironmentSetup { return @{Name = "12345"}, @{Name = "12346" }, @{Name = "12348"}, @{Name = "Foobar12349"} }
The call in my system under test started returning the expected values, the same as returning a plain string.
Using a leading comma didn't work, and casting the Should Be call didn't work either.