You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have some functions that contain their own inner functions. These inner functions (I.E. the functions inside functions) do random stuff that's only needed for that one outer function. I would like to test the different outer function's logic paths, but to do this, I need to be able to mock the inner function to return different stuff.
Does anyone know a good way to do this?
Barring the opinionated debates... Is this bad practice or is there a reason I SHOULDN'T have inner functions like this? It makes the code easier to read in my opinion. And my module already has a TON of stuff.. I hate creating a whole new file just for one function to use.
Example:
functionInnerFunctions {
functioninsideFunction {
Write-Output2
}
$output= insideFunction
if ($output-eq2) {"Do this"}
else {"Do that"}
}
$here=Split-Path-Parent $MyInvocation.MyCommand.Path$sut= (Split-Path-Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.",".")
."$here\$sut"
Describe "InnerFunctions" {
It "does something useful" {
InnerFunctions | Should Be "Do This"
}
}
In this example, I would like to test to make sure InnerFunctions will return "Do That" when the output of insideFunction is NOT 2.
Thanks in advance!
The text was updated successfully, but these errors were encountered:
Unfortunately, there's no way for Pester to deal with that. You can't mock the inner function before it's been loaded, and even if you tried, as soon as you run the outer function, it creates a new version of the inner function farther down the stack.
While this isn't a bad practice as far as PowerShell is concerned, it does make testing difficult, so I personally don't nest functions like that.
This may be more of a question than an issue...
I have some functions that contain their own inner functions. These inner functions (I.E. the functions inside functions) do random stuff that's only needed for that one outer function. I would like to test the different outer function's logic paths, but to do this, I need to be able to mock the inner function to return different stuff.
Does anyone know a good way to do this?
Barring the opinionated debates... Is this bad practice or is there a reason I SHOULDN'T have inner functions like this? It makes the code easier to read in my opinion. And my module already has a TON of stuff.. I hate creating a whole new file just for one function to use.
Example:
In this example, I would like to test to make sure InnerFunctions will return "Do That" when the output of insideFunction is NOT 2.
Thanks in advance!
The text was updated successfully, but these errors were encountered: