Pester icon indicating copy to clipboard operation
Pester copied to clipboard

Mock Fails With CommandNotFoundException or Does Not Mock the Call

Open RickFlist opened this issue 10 years ago • 2 comments

I've been troubleshooting this CommandNotFoundException for the last few days and I'm a bit stumped. This error seems to occur intermittently. The only consistency I've found is that the error seems to crop up when I use the -ModuleName parameter on the Mock to indicate 'Carbon' as the source module for the mocked command.

Modules Loaded: `ModuleType Version Name


Script 2.1.0   Carbon                         
Script 1.0.0.0 ISE                            

Manifest 3.1.0.0 Microsoft.PowerShell.Management Manifest 3.0.0.0 Microsoft.PowerShell.Security
Manifest 3.1.0.0 Microsoft.PowerShell.Utility
Manifest 3.0.0.0 Microsoft.WSMan.Management
Script 3.3.14 Pester
Script 1.0 WhsAutomation
Script 1.0 WhsSecretStore
Script 1.0 WhsSettings
`

Error Message: [-] Error occurred in Context block 73ms CommandNotFoundException: Could not find Command Get-FileShare at Validate-Command, C:\Program Files\WindowsPowerShell\Modules\Pester\3.3.14\Functions\Mock.ps1: line 770 at Mock, C:\Program Files\WindowsPowerShell\Modules\Pester\3.3.14\Functions\Mock.ps1: line 168 at , C:\Source\Arc\WhsSettings\Test\Get-ArtifactRepositoryRoot.Tests.ps1: line 39

Script: `& (Join-Path -Path $PSScriptRoot -ChildPath ....\WhsAutomation\Import-WhsAutomation.ps1 -Resolve)

Describe 'Test-GetArtifactRepositoryRoot' { BeforeEach { $Global:Error.Clear() } AfterEach {}

Context 'No Errors' {

    Mock -ModuleName WhsSettings `
         -CommandName Get-WhsSetting `
         -MockWith { return 'Server01' } `
         -ParameterFilter { $Name -eq 'ArtifactRepositoryHostName' }

    Mock -ModuleName WhsSettings `
         -CommandName Get-WhsSetting `
         -MockWith { return 'FirstShare' } `
         -ParameterFilter { $Name -eq 'ArtifactRepositoryShareName' }

    It 'Should Return Artifacts UNC' {
        $artyPath = Get-ArtifactRepositoryRoot -Environment 'dev'
        $Global:Error.Count | Should Be 0
        $artyPath | Should Not BeNullOrEmpty
    }

    It 'No Errors When Path Is Hostname Slash Share' {
        $artyPath = Get-ArtifactRepositoryRoot -Environment TurtleBeach
        $Global:Error.Count | Should Be 0
        $artyPath | Should Not BeNullOrEmpty
        ([Uri]$artyPath).Segments.Count | Should Be 2
    }

    Mock -ModuleName WhsSettings `
         -CommandName Get-WhsSetting `
         -MockWith { return 'SecondShare' } `
         -ParameterFilter { $Name -eq 'ArtifactRepositoryName' }

    Mock Get-FileShare { return ([PSCustomObject]@{ Name='FirstShare';Path='C:\DummyArtifactsFolder';Description='' }) }

    It 'No Errors When Path Is Host Slash Share Slash Share' {
        $artyPath = Get-ArtifactRepositoryRoot -Environment TurtleBeach
        $Global:Error.Count | Should Be 0
        $artyPath | Should Not BeNullOrEmpty
        ([Uri]$artyPath).Segments.Count | Should Be 3

    }
}

Context 'Expected Errors' {

    Mock -ModuleName WhsSettings `
         -CommandName Get-WhsSetting `
         -MockWith { return '' } `
         -ParameterFilter { $Name -eq 'ArtifactRepositoryHostName' }

    Mock -ModuleName WhsSettings `
         -CommandName Get-WhsSetting `
         -MockWith { return '' } `
         -ParameterFilter { $Name -eq 'ArtifactRepositoryShareName' }

    It 'One Error When Artifact Repository Host Name Is Empty' {
        $artyPath = Get-ArtifactRepositoryRoot -Environment TurtleBeach
        $Global:Error.Count | Should BeGreaterThan 0
        $artyPath | Should BeNullOrEmpty
    }

    It 'One Error When Artifact Repository Share Name Is Empty' {
        $artyPath = Get-ArtifactRepositoryRoot -Environment TurtleBeach
        $Global:Error.Count | Should BeGreaterThan 0
        $artyPath | Should BeNullOrEmpty

    }
}

} `

RickFlist avatar Jan 27 '16 22:01 RickFlist

If you're calling Carbon's Get-FileShare command, then you would not pass -ModuleName Carbon when mocking it. Rather, the module referred to by -ModuleName is the module that's calling Get-FileShare. (This is a common point of confusion... maybe we should rename the parameter to something like -FromModule.)

That said, I'm not sure what would be causing this. If you have the Carbon module loaded (which your Get-Module output says you do), you should never wind up with a "command not found" error for one of its exported functions. Even if you do use -ModuleName Carbon, the Mock command should still work (though you would probably find that calls to the function didn't work the way you expected them to.)

dlwyatt avatar Jan 28 '16 14:01 dlwyatt

This is what we found out. It was an RTFM moment. If you look at the Get-Help for the -ModuleName parameter, it explains this:

Optional string specifying the name of the module where this command is to be mocked. This should``be a module that _calls_ the mocked command; it doesn't necessarily have to be the same module``which originally implemented the command.

I agree that the parameter should be renamed to make it clearer.

Thanks!

RickFlist avatar Jan 28 '16 19:01 RickFlist