Skip to content

Recommended way to test internal/private functions of a module? #2109

Answered by nohwnd
J0F3 asked this question in Q&A
Discussion options

You must be logged in to vote

InModuleScope is a bit problematic in Pester 5. But it can still be used if you really need it and understand the limitations.

One case where you should avoid InModuleScope is when you are testing a public function, that calls an internal function, and you need to mock that internal function. In that case Mock Some-InternalFunction -ModuleName <your module>, should be used instead, because then only the Mock is inserted into the module, but your test code is invoked from the outside of the module. This allows you to inject mocks, without exposing all the module internals to the test = you are sure you publish only what you meant to publish.

If you are testing an internal function, then us…

Replies: 1 comment 6 replies

Comment options

You must be logged in to vote
6 replies
@nohwnd
Comment options

@J0F3
Comment options

@maccluu
Comment options

@schlich
Comment options

@maccluu
Comment options

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