Functions/Block-UntilExchangeOnlineGroupAvailable.Tests.ps1
describe "BitTitan.Runbooks.ExchangeOnline/Block-UntilExchangeOnlineGroupAvailable" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\Block-UntilExchangeOnlineGroupAvailable.ps1" # Declare external functions and mocks function Get-Group { param ($Identity) } function Get-DynamicDistributionGroup { param ($Identity) } context "when the group is immediately available" { # Declare mocks mock Get-Group { return [PSCustomObject]@{ GroupPrincipalName = "group@domain.com" } } it "returns the distribution group after trying once" { # Call the function $output = Block-UntilExchangeOnlineGroupAvailable -Identity "group@domain.com" -WaitLimitSeconds 20 -WaitIntervalSeconds 1 # Verify the mocks Assert-MockCalled Get-Group -Times 1 -Exactly -ParameterFilter { $Identity -eq "group@domain.com" } -Scope it # Verify the output $output.GroupPrincipalName | Should Be "group@domain.com" } # Declare mocks mock Get-DynamicDistributionGroup { return [PSCustomObject]@{ GroupPrincipalName = "group@domain.com" } } it "returns the dynamic distribution group after trying once" { # Call the function $output = Block-UntilExchangeOnlineGroupAvailable -Identity "group@domain.com" -GroupType "DynamicDistributionGroup" -WaitLimitSeconds 20 # Verify the mocks Assert-MockCalled Get-DynamicDistributionGroup -Times 1 -Exactly -ParameterFilter { $Identity -eq "group@domain.com" } -Scope it # Verify the output $output.GroupPrincipalName | Should Be "group@domain.com" } } context "when the group is available after several tries" { # Declare mocks $Global:numTries = 0 mock Get-Group { ++$Global:numTries if ($Global:numTries -ge 5) { return [PSCustomObject]@{ GroupPrincipalName = "group@domain.com" } } } it "returns the group after trying several times" { # Call the function $output = Block-UntilExchangeOnlineGroupAvailable -Identity "group@domain.com" -WaitLimitSeconds 20 # Verify the mocks Assert-MockCalled Get-Group -Times 5 -Exactly -ParameterFilter { $Identity -eq "group@domain.com" } -Scope it # Verify the output $output.GroupPrincipalName | Should Be "group@domain.com" } } context "when the group is not available" { # Declare mocks mock Get-Group {} it "returns null and outputs an error" { # Call the function $output = Block-UntilExchangeOnlineGroupAvailable -Identity "group@domain.com" -WaitLimitSeconds 5 ` -ErrorAction SilentlyContinue -ErrorVariable errorVariable # Verify the output $errorVariable | Should Not BeNullOrEmpty $output | Should Be $null } } } |