Martin
Martin

Reputation: 535

Passing Context ForEach variables to It blocks

Why are the variables in my Context -ForEach cases not being passed to the It blocks underneath the Context? I have this code in my test file.

BeforeAll {
    Import-Module Pester
    Import-Module MyModule
}

Describe 'MyFunction' {
    BeforeAll {
        Mock -ModuleName MyModule Invoke-RestMethod { return @{ access_token = 'mocked access token' } }
    }

    Context 'When the service is <service>' -ForEach @(
        @{Service = 'MicrosoftGraph'}
        @{Service = 'SharePointOnline'}
    ) {
        It 'Should return an access token' {
            InModuleScope MyModule {
                Mock New-GraphJwtAssertion {}
                $TenantId = '<tenantId>'
                $ClientId = '<clientId>'
                $Certificate = <certificate>
                $result = MyFunction -TenantId $TenantId -ClientId $ClientId -Certificate $Certificate -Service $Service

                $result | Should -Be 'mocked access token'
            }
        }
    }
}

I suspect the issue might have to do with the InModuleScope block which I'm using because the New-GraphJwtAssertion function is a private function of the module.

The Service parameter on MyFunction uses a ValidateSet attribute and when I run the tests I just get an error that "The argument "" does not belong to the set <set values>".

So the Service variable from the ForEach hashtable doesn't appear to be passed to the It block.

What do I need to change in this test to be able to pass those variables through to the It blocks so they can be used.

Upvotes: 0

Views: 68

Answers (0)

Related Questions