InModuleScope
Contributions are welcome in Pester-repo.
SYNOPSIS
Allows you to execute parts of a test script within the scope of a PowerShell script or manifest module.
SYNTAX
InModuleScope [-ModuleName] <string> [-ScriptBlock] <scriptblock> [[-Parameters] <hashtable>]
[[-ArgumentList] <Object[]>]
DESCRIPTION
By injecting some test code into the scope of a PowerShell script or manifest module, you can use non-exported functions, aliases and variables inside that module, to perform unit tests on its internal implementation.
InModuleScope may be used anywhere inside a Pester script, either inside or outside a Describe block.
EXAMPLES
EXAMPLE 1
# The script module:
function PublicFunction {
# Does something
}
function PrivateFunction {
return $true
}
Export-ModuleMember -Function PublicFunction
# The test script:
BeforeAll {
Import-Module MyModule
}
Describe 'Testing MyModule' {
It 'Tests the Private function' {
InModuleScope MyModule {
PrivateFunction | Should -Be $true
}
}
}
Normally you would not be able to access "PrivateFunction" from the PowerShell session, because the module only exported "PublicFunction". Using InModuleScope allowed this call to "PrivateFunction" to work successfully.
EXAMPLE 2
# The script module:
function PublicFunction {
# Does something
}
function PrivateFunction ($MyParam) {
return $MyParam
}
Export-ModuleMember -Function PublicFunction
# The test script:
Describe 'Testing MyModule' {
BeforeAll {
Import-Module MyModule
}
It 'passing in parameter' {
$SomeVar = 123
InModuleScope 'MyModule' -Parameters @{ MyVar = $SomeVar } {
$MyVar | Should -Be 123
}
}
It 'accessing whole testcase in module scope' -TestCases @(
@{ Name = 'Foo'; Bool = $true }
) {
# Passes the whole testcase-dictionary available in $_ to InModuleScope
InModuleScope 'MyModule' -Parameters $_ {
$Name | Should -Be 'Foo'
PrivateFunction -MyParam $Bool | Should -BeTrue
}
}
}
This example shows two ways of using -Parameters to pass variables created in a
testfile into the module scope where the scriptblock provided to InModuleScope is executed.
No variables from the outside are available inside the scriptblock without explicitly passing
them in using -Parameters or -ArgumentList.
PARAMETERS
-ArgumentList
A optional list of arguments to be passed to the scriptblock.
Type: System.Object[]
DefaultValue: '@()'
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
Position: 3
IsRequired: false
ValueFromPipeline: false
ValueFromPipelineByPropertyName: false
ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''
-ModuleName
The name of the module into which the test code should be injected. This module must already be loaded into the current PowerShell session.
Type: System.String
DefaultValue: ''
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
Position: 0
IsRequired: true
ValueFromPipeline: false
ValueFromPipelineByPropertyName: false
ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''
-Parameters
A optional hashtable of parameters to be passed to the scriptblock. Parameters are automatically made available as variables in the scriptblock.
Type: System.Collections.Hashtable
DefaultValue: '@{}'
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
Position: 2
IsRequired: false
ValueFromPipeline: false
ValueFromPipelineByPropertyName: false
ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''
-ScriptBlock
The code to be executed within the script or manifest module.
Type: System.Management.Automation.ScriptBlock
DefaultValue: ''
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
Position: 1
IsRequired: true
ValueFromPipeline: false
ValueFromPipelineByPropertyName: false
ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''
CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.
INPUTS
OUTPUTS
NOTES
RELATED LINKS
VERSION
This page was generated using comment-based help in Pester 6.0.0.