Invoke.Tests.ps1
#################################################################################################### # Declarations #################################################################################################### # Set InformationPreference $InformationPreference = "Continue" #################################################################################################### # Functions #################################################################################################### # Add modules folder to path if not already added if (!$env:PSModulePath.EndsWith(";$(Get-Location)\Modules")) { $env:PSModulePath += ";$(Get-Location)\Modules" } # Import functions from BitTitan.Runbooks.Modules Import-Module BitTitan.Runbooks.Common -Force #################################################################################################### # The tests #################################################################################################### describe "Modules/BitTitan.Runbooks.Common/Invoke/Invoke-TryCatchVerifyForScriptBlock" -Tags "module", "common", "unit" { context "when no output is expected from the script block" { it "invokes the script block with no exceptions" { # Call the function $result = Invoke-TryCatchVerifyForScriptBlock { } "generate no output" # Verify the output $result.Output | Should Be $null $result.ErrorMessage | Should Be $null } it "invokes the script block which throws an exception" { # Call the function $result = Invoke-TryCatchVerifyForScriptBlock { throw "exceptionMessage" } "generate no output" -ErrorAction SilentlyContinue # Verify the output $result.Output | Should Be $null $result.ErrorMessage | Should BeLike "Exception during 'generate no output'.*exceptionMessage*" } } context "when output is expected from the script block but the expected output is not specified" { it "invokes the script block with output and no exceptions" { # Call the function $result = Invoke-TryCatchVerifyForScriptBlock { "test" } "retrieve test" -ShouldHaveOutput # Verify the output $result.Output | Should Be "test" $result.ErrorMessage | Should Be $null } it "invokes the script block which throws an exception" { # Call the function $result = Invoke-TryCatchVerifyForScriptBlock { throw "exceptionMessage" } "retrieve test" -ShouldHaveOutput -ErrorAction SilentlyContinue # Verify the output $result.Output | Should Be $null $result.ErrorMessage | Should BeLike "Exception during 'retrieve test'.*exceptionMessage*" } it "invokes the script block with no output and no exceptions" { # Call the function $result = Invoke-TryCatchVerifyForScriptBlock { } "retrieve test" -ShouldHaveOutput -ErrorAction SilentlyContinue # Verify the output $result.Output | Should Be $null $result.ErrorMessage | Should Be "Failed to 'retrieve test' - no output when output is expected." } } context "when the expected result from the script block is specified" { it "invokes the script block with correct output and no exceptions" { # Call the function $result = Invoke-TryCatchVerifyForScriptBlock { "test" } "retrieve test" -ExpectedOutput "test" # Verify the output $result.Output | Should Be "test" $result.ErrorMessage | Should Be $null } it "invokes the script block which throws an exception" { # Call the function $result = Invoke-TryCatchVerifyForScriptBlock { throw "exceptionMessage" } "retrieve test" -ExpectedOutput "test" -ErrorAction SilentlyContinue # Verify the output $result.Output | Should Be $null $result.ErrorMessage | Should BeLike "Exception during 'retrieve test'.*exceptionMessage*" } it "invokes the script block with incorrect output and no exceptions" { # Call the function $result = Invoke-TryCatchVerifyForScriptBlock { "testing" } "retrieve test" -ExpectedOutput "test" -ErrorAction SilentlyContinue # Verify the output $result.Output | Should Be "testing" $result.ErrorMessage | Should Be "Failed to 'retrieve test' - output does not match expected output." } } } |