< Back

InModuleScope

Wed Jan 30, 2019 5:56 pm

NAME InModuleScope



SYNOPSIS

Allows you to execute parts of a test script within the

scope of a PowerShell script module.





SYNTAX

InModuleScope [-ModuleName] <String> [-ScriptBlock] <ScriptBlock> [<CommonParameters>]





DESCRIPTION

By injecting some test code into the scope of a PowerShell

script 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.





PARAMETERS

-ModuleName <String>

The name of the module into which the test code should be

injected. This module must already be loaded into the current

PowerShell session.



Required? true

Position? 1

Default value

Accept pipeline input? false

Accept wildcard characters? false



-ScriptBlock <ScriptBlock>

The code to be executed within the script module.



Required? true

Position? 2

Default value

Accept pipeline input? false

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS



OUTPUTS



-------------------------- EXAMPLE 1 --------------------------



PS C:\\># The script module:



function PublicFunction

{

# Does something

}



function PrivateFunction

{

return $true

}



Export-ModuleMember -Function PublicFunction



# The test script:



Import-Module MyModule



InModuleScope MyModule {

Describe 'Testing MyModule' {

It 'Tests the Private function' {

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.











RELATED LINKS