Skip to content

Mocking Invoke-RestMethod -StatusCodeVariable #2375

Closed Answered by fflaten
dkaszews asked this question in Q&A
Discussion options

You must be logged in to vote

This a great question! There's two versions of this answer: one for your sample and one for your production code.
Your sample should work when you find the right scope-level to use with Set-Variable. Example:

Describe 'DownloadStuff in same session state' {
    BeforeAll {
        function DownloadStuff {
            # $demoVar = 123
            $stuff = Invoke-RestMethod 'example.com/api/stuff' -SkipHttpErrorCheck -StatusCodeVariable scv
            @( $scv, $stuff )
        }
    }
    Context 'Forbidden' {
        BeforeEach {
            Mock Invoke-RestMethod {
                # We need to find the correct Scope-level for DownloadStuff
                # 1. Uncomment $demoVar in Downl…

Replies: 4 comments 5 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by dkaszews
Comment options

You must be logged in to vote
1 reply
@fflaten
Comment options

Comment options

You must be logged in to vote
1 reply
@fflaten
Comment options

Comment options

You must be logged in to vote
3 replies
@fflaten
Comment options

@fflaten
Comment options

@dkaszews
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants