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
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 functionsfunctionGetContentWithQualifiedName {
$content= Microsoft.Powershell.Management\Get-Content somefile.txt
return$content
}
functionGetContentWithUnqualifiedName {
$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
}
}
The text was updated successfully, but these errors were encountered:
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.
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.
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:The text was updated successfully, but these errors were encountered: