Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing Inner Functions #414

Closed
nhudacin opened this issue Sep 30, 2015 · 2 comments
Closed

Testing Inner Functions #414

nhudacin opened this issue Sep 30, 2015 · 2 comments

Comments

@nhudacin
Copy link

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:

function InnerFunctions {

    function insideFunction {
        Write-Output 2
    }

    $output = insideFunction

    if ($output -eq 2) {"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!

@dlwyatt
Copy link
Member

dlwyatt commented Sep 30, 2015

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.

@nhudacin
Copy link
Author

Thank you @dlwyatt !!!

I'm going to close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants