0

I run the following test with Invoke-Pester. The 'function' tests fine but the 'workflow' fails. Is there any way for the test to see the calls within the InlineScript? Thanks.

BeforeAll {
    function TestFunction {
        Write-Host  "Executing function"
        Get-Host
    }

    workflow TestWorkflow {

        InlineScript {
            Write-Host  "Executing inlinescript"            
            Get-Host
        }
    }
}

Describe "Test-Workflow" {
    BeforeAll {
        Mock Get-Host { "Mocked Get-Host" }
    }
    
    Context 'test' {
        It 'workflow' {
            TestWorkflow
            Should -Invoke -Command Get-Host -Exactly 1
        }

        It 'function' {
            TestFunction
            Should -Invoke -Command Get-Host -Exactly 1
        }

    }
}

The test results:

[-] Test-Workflow.test.workflow 1.53s (1.53s|4ms)
 Expected Get-Host to be called 1 times exactly, but was called 0 times
 at Should -Invoke -Command Get-Host -Exactly 1, C:\Test-Workflow.Tests.ps1:25
 at <ScriptBlock>, C:\Test-Workflow.Tests.ps1:25
Executing function
Tests completed in 1.97s
Tests Passed: 1, Failed: 1, Skipped: 0 NotRun: 0
3
  • Add logging. See : pester.dev/docs/usage/troubleshooting. Since error says Get-Host was called 0 times the error must be happening be with either the Write-Host or Get-Host. Probably the connection is not completing or authentication is failing. Commented Sep 4, 2023 at 1:51
  • I am running on localhost only, removing Write-Host doesn't affect the problem. If the error was within Get-Host I would think that would be counted as a call. I did replace Get-Host with Get-Date, same failure. Logging shows "Mock: Mock for Get-Host was invoked from block Process, resolving call history and behaviors." in the case of the 'function' It but this entry is missing from the 'workflow' It. I can only guess Mock is simply unable to detect calls in the InlineScript Commented Sep 4, 2023 at 12:51
  • Maybe following will help : pester.dev/docs/usage/mocking Commented Sep 4, 2023 at 13:04

1 Answer 1

0

Mocking is not possible in workflows as they are executed in a separate process by Windows Workflow Foundation. Pester has no control or state in that process.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.