Reputation: 1
In my module PSCHSGActiveDirectory, I have a function Set-CHSGActiveUserActiveOn which call 2 functions from ActiveDirectory module : Get-ADUser and Enable-ADAccount.
Function Set-CHSGActiveUserActiveOn() {
Param(
[Parameter(
Mandatory = $True,
ValueFromPipeline = $True
)]
[String]$samAccountName
)
...
$user = Get-ADUser -Identity $SamAccountName
...
Enable-ADAccount -Identity $user
...
}
Here, my pester test file :
BeforeAll {
Import-Module PSCHSGActiveDirectory -Force
}
Describe 'Set-CHSGActiveUserActiveOn' {
Context 'Without Reset Password' {
BeforeAll {
#Mock Get-ADUser -ModuleName PSCHSGActiveDirectory { }
#Mock Enable-ADAccount -ModuleName PSCHSGActiveDirectory { }
}
It 'Tests Enable User' {
PSCHSGActiveDirectory\Set-CHSGActiveUserActiveOn -SamAccountName 'johndoe' | Should -BeNullOrEmpty
}
}
}
When i didn't mock anything, test failed on Get-ADUser.
When i mock only Get-ADUser, test failed on Enable-ADAccount with the message :
[-] Set-CHSGActiveUserActiveOn.Without Reset Password.Tests Enable User 42ms (40ms|2ms)
ValidationMetadataException: L’argument est Null. Spécifiez une valeur valide pour l’argument, puis réessayez.
ParameterBindingValidationException: Impossible de valider l'argument sur le paramètre «*Identity*». L’argument est Null. Spécifiez une valeur valide pour l’argument, puis réessayez.
When i mock both, test still failed on Enable-ADAccount with the same message
I try to change my function Set-CHSGActiveUserActiveOn with the use of a param Microsoft.ActiveDirectory.Management.ADUser instead of a string (and delete the Get-ADuser of course) but the problem still here.
I use these versions :
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Manifest 1.0.1.0 ActiveDirectory {Add-ADCentralAccessPolicyMember, Add-ADComputerServiceAccount, Add-ADDomainControllerPasswordReplicationPolicy, Add-ADFineGrainedPasswordPolicySubject...}
Binary 1.0.0.0 CimCmdlets {Export-BinaryMiLog, Get-CimAssociatedInstance, Get-CimClass, Get-CimInstance...}
Manifest 3.1.0.0 Microsoft.PowerShell.Management {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Content...}
Manifest 3.0.0.0 Microsoft.PowerShell.Security {ConvertFrom-SecureString, ConvertTo-SecureString, Get-Acl, Get-AuthenticodeSignature...}
Manifest 3.1.0.0 Microsoft.PowerShell.Utility {Add-Member, Add-Type, Clear-Variable, Compare-Object...}
Manifest 3.0.0.0 Microsoft.WSMan.Management {Connect-WSMan, Disable-WSManCredSSP, Disconnect-WSMan, Enable-WSManCredSSP...}
Script 5.3.1 Pester {Add-ShouldOperator, AfterAll, AfterEach, Assert-MockCalled...}
Script 3.4.0 Pester {AfterAll, AfterEach, Assert-MockCalled, Assert-VerifiableMocks...}
Script 2.0.0 PSReadline {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PSReadLineKeyHandler, Set-PSReadLineKeyHandler...}
I suppose it is just a syntax problem to mock Enable-ADAccount but i can't find any ways.
Thank you in advance,
Upvotes: 0
Views: 259
Reputation: 18051
If the flow of the tested function expects a result from a called cmdlet, you should always return (a mocked) result from that function.
Change the Mocks to something like this:
Mock Get-ADUser -ModuleName PSCHSGActiveDirectory {
return $Identity
}
Mock Enable-ADAccount -ModuleName PSCHSGActiveDirectory { }
$Identity
is the value of the -Identity
parameter as made available within the Mock by Pester. You could also return a fixed value, eg. "user"
or another (script) variable
Upvotes: 0