-
-
Notifications
You must be signed in to change notification settings - Fork 476
Closed
Labels
Description
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
}
}