I am trying to wrap my head around Pester with a relatively simple script that sets the HomeDirectory attribute of an AD account, and optionally sends output to a log file. The script uses Get-ADUser and Set-ADUser, so I am trying to Mock these in my Pester tests. I am not mocking or testing the logging function, and instead let the test create a real log file.
When I run my tests, they all fail with something like:
Expected: {True} But was: {}
My log shows the output I expect.
Here is an example of one of three Contexts in my Describe block:
Describe "Set-TheHomeFolder" { Mock Set-ADUser {} Context "When HomeDirectory empty" { Mock Get-ADUser { return @{ 'DistinguishedName' = 'CN=TesUser,OU=Users,DC=domain,DC=com'; 'Enabled' = $True; 'GivenName' = 'Test'; 'HomeDirectory' = ''; 'SamAccountName' = 'TestUser'; 'Surname' = 'User' } } $result = Set-TheHomeFolder -samAccountName TestUser It "Confirms HomeDirectory value is empty" { $HasHomeDirectory | Should Be $false } } ... }
I have attached sanitized versions of my script, tests. My log looks like this:
“SamAccountName”,“TimeStamp”,“CurrentHomeDirectory”,“NewHomeDirectory”,“HasHomeDirectory”,“DirectoriesMatch”
“TestUser”,“5/11/2015 3:18:41 PM”,“\files\users\TestUser”,“\files\TestUser”,“True”,“False”
“TestUser”,“5/11/2015 3:18:41 PM”,“\files\TestUser”,“\files\TestUser”,“True”,“True”
“TestUser”,“5/11/2015 3:18:41 PM”,“”,“\files\TestUser”,“True”,“False”
Edited: Splats were missing semicolons.