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

Mocks are not hit if the mocked command is invoked with a qualified name #308

Closed
eliah-hecht-zocdoc opened this issue Apr 16, 2015 · 3 comments

Comments

@eliah-hecht-zocdoc
Copy link

If I mock a function and then my production code calls that function with a qualified name (like SomeModule\SomeFunction), the actual function is invoked, not the mock. Here's a test case to demonstrate:

# production functions
function GetContentWithQualifiedName {
    $content = Microsoft.Powershell.Management\Get-Content somefile.txt

    return $content
}

function GetContentWithUnqualifiedName {
    $content = Get-Content somefile.txt

    return $content
}

# tests
Describe "GetContentWithQualifiedName" {
    It "returns the mocked content" {
        # neither call to Mock is effective
        Mock Get-Content {return "foo"}
        Mock Microsoft.Powershell.Management\Get-Content {return "foo"}

        $result = GetContentWithQualifiedName

        $result | Should Match "foo" # Fails
    }
}

Describe "GetContentWithUnqualifiedName" {
    It "returns the mocked content" {
        Mock Get-Content {return "foo"}

        $result = GetContentWithUnqualifiedNAme

        $result | Should Match "foo" # Passes
    }
}
@dlwyatt
Copy link
Member

dlwyatt commented Apr 16, 2015

Yep. Unfortunately, I don't think there's anything we can do about that while still maintaining PowerShell 2.0 compatibility. I believe PowerShell 3.0 added some hooks that we can use to improve on this in the future, though.

@eliah-hecht-zocdoc
Copy link
Author

I was afraid this might be the case. I'll add a note about this to the Mock wiki page, so people don't have to spend time trying to debug this issue in the future.

@nohwnd
Copy link
Member

nohwnd commented Dec 14, 2018

This works because we are adding alias with the unqualified and fully qualified name.

@nohwnd nohwnd closed this as completed Dec 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants