3

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.

Josh R
  • 1,970
  • 3
  • 27
  • 45
  • 1
    Is PowerShell unrolling the array automatically? Try forcing it with a unary comma: `return ,$fakeListingOfDirectories` – briantist Dec 09 '15 at 04:08
  • I think it's something different, I tried your suggestion but the mocked Get-ChildItem function still returns nothing when called inside the EnvironmentSetup module. I'm updating my question with a more detailed follow up to this. – Josh R Dec 09 '15 at 18:57
  • FWIW it's a lot quicker to arrive at a good solution if you can provide an [MCVE](https://stackoverflow.com/help/mcve) in your question. It took me a while to figure out how to do that effectively for Pester. Basically you need to provide the body of two files: The SUT and the test. Here is [an example of a Pester MCVE](https://github.com/pester/Pester/issues/305#issue-68550923). – alx9r Dec 10 '15 at 02:36

2 Answers2

1

How about casting to System.IO.DirectoryInfo

Get-HighestBuildNumber -buildRoot "C:\my\test\directory" | Should Be @([System.IO.DirectoryInfo]"12348")

or using the "Name" property

return $fakeListingOfDirectories.Name

?

Jaqueline Vanek
  • 1,100
  • 10
  • 20
1

I'm not at a machine where I can test this, but the Mock {} scriptblock is likely not in the same scope as the Describe {} scriptblock. I would rewrite your Describe{} block like this:

Describe "Get-HighestBuildNumber" {
    Context "Get-ChildItem mocked to returns 12345, 12346, 12348, Foobar12349" {
        Mock -ModuleName EnvironmentSetup Get-ChildItem { 
            @( 
                "12345","12346","12348","Foobar12349" | 
                    % { New-Object System.IO.DirectoryInfo($_) }
            )
        }
        it "should return 12348" {
            Get-HighestBuildNumber -buildRoot "C:\my\test\directory" | Should Be "12348"
        }
    }
}
alx9r
  • 3,675
  • 4
  • 26
  • 55