Fail to mock Get-WindowsFeature

596 views
Skip to first unread message

Jafar Soltani

unread,
Aug 7, 2015, 9:10:36 AM8/7/15
to Pester
Hello,
I have as script that uses Get-WindowsFeature and I'm trying to write some tests using pester and I need to mock Get-WindowsFeature. If I run pester locally on my Win 8.1 machine it works fine but if I run it as part of a job on TC agent running on Win server 2012R2 it failes with following error:

Could not find Command Get-WindowsFeature
at line: 713 in D:\Teamcity\work\dev\contrib\PowerShell\Pester-3.3.9\Functions\Mock.ps1

Here is my test
It 'On Windows Server 2012R2 check fails when component is not installed' {
            $mode
= 'Check'
           
Mock Get-WindowsFeature {
                param
(
               
[string] $Name
               
)      
             
[PSCustomObject]@{Installed = $false}
           
}
             
           
Manage-ActiveDirectoryModuleOnWindowsServer2012R2 | should Be $false
           
Assert-MockCalled Get-WindowsFeature -Scope It
       
}

Does anyone has any suggestion how can I debug this by adding extra logging message in mock.ps1 file

Dave Wyatt

unread,
Aug 7, 2015, 12:09:36 PM8/7/15
to Jafar Soltani, Pester
Which line is 713 in your code snippet?

Also, on a side note, your mock script block should not contain a param() block.  The parameters from the command you're mocking are automatically inherited.  So you can just do this:

    Mock Get-WindowsFeature { [PSCustomObject]@{Installed = $false} }

--
You received this message because you are subscribed to the Google Groups "Pester" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pester+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jafar Soltani

unread,
Aug 7, 2015, 1:23:40 PM8/7/15
to Pester, jafars...@gmail.com
If I understand your question correctly, that's line 713 in mock.ps1 file in pester source code

if (-not $commandInfo.Command) {
   
throw ([System.Management.Automation.CommandNotFoundException] "Could not find Command $commandName")
}

Dave Wyatt

unread,
Aug 7, 2015, 1:53:48 PM8/7/15
to Jafar Soltani, Pester
Whoops, missed that.  :)  That's odd.  What happens if you run Get-Command Get-WindowsFeature on the server where you got that error?

Jafar Soltani

unread,
Aug 7, 2015, 2:52:30 PM8/7/15
to Pester, jafars...@gmail.com
Managed to track down what the issue is. It's caused by running 32bit version of Powershell as opposed to 64bit version, running it as a 32bit I get:

Get-Command : The term 'Get-WindowsFeature' is not recognized as the name of a
cmdlet, function, script file, or operable program. Check the spelling of the
name, or if a path was included, verify that the path is correct and try again.


whereas running it as a 64bit I get:

CommandType     Name                                               ModuleName  
-----------     ----                                               ----------  
Cmdlet          Get-WindowsFeature                                 ServerMan...

Thanks.

Dexter Dhami

unread,
Oct 24, 2015, 7:02:32 AM10/24/15
to Pester, jafars...@gmail.com
You can overcome this issue by using BeforeEach {} inside the Context block to actually load a Dummy Module which has the Get-WindowsFeature function exported.
At least this way, Pester will see a Function which it will be able to mock.

I recently used this trick to run Pester tests in my local machine which didn't have AD PowerShell module installed. See below :

001
002
003
004
005
006
007
008
009
010
011
012
013
014

BeforeEach {
    # Create a Dummy AD module and import it
    $DummyModule = New-Module -Name ServerManager  -Function "Get-WindowsFeature" -ScriptBlock {
                                                                    Function Get-WindowsFeature {"Get-WindowsFeature"} ;
                                                                }
    $DummyModule| Import-Module  
}

AfterEach {
    # Forcefully remove the Dummy AD Module
    Remove-Module -Name ServerManager -Force  -ErrorAction SilentlyContinue
}

Dexter Dhami

unread,
Oct 24, 2015, 7:04:22 AM10/24/15
to Pester, jafars...@gmail.com
Sorry the comments still say load the dummy AD Module :)
The code sample is from my tests.
Reply all
Reply to author
Forward
0 new messages