Invoke.Tests.ps1
#################################################################################################### # Declarations #################################################################################################### # Set InformationPreference $InformationPreference = "Continue" #################################################################################################### # Functions #################################################################################################### # Import other BitTitan.Runbooks modules Import-BT_Module BitTitan.Runbooks.Common #################################################################################################### # The tests #################################################################################################### describe "Modules/BitTitan.Runbooks.Common/Invoke/Invoke-ScriptBlock" -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-ScriptBlock "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-ScriptBlock "generate no output" { throw "exceptionMessage" } -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-ScriptBlock "retrieve test" { "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-ScriptBlock "retrieve test" { throw "exceptionMessage" } -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-ScriptBlock "retrieve test" { } -ShouldHaveOutput -ErrorAction SilentlyContinue # Verify the output $result.Output | Should Be $null $result.ErrorMessage | Should Be "Failed to 'retrieve test' - no output when an 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-ScriptBlock "retrieve test" { "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-ScriptBlock "retrieve test" { throw "exceptionMessage" } -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-ScriptBlock "retrieve test" { "testing" } -ExpectedOutput "test" -ErrorAction SilentlyContinue # Verify the output $result.Output | Should Be "testing" $result.ErrorMessage | Should Be "Failed to 'retrieve test' - the output does not match the expected output." } } } |