Skip to content

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

@eliah-hecht-zocdoc

Description

@eliah-hecht-zocdoc

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
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions