# Understanding Mocking

**URL:** <https://forums.powershell.org/t/understanding-mocking/10125>\
**Category:** Pester\
**Created:** [February 14, 2018, 3:36pm UTC](https://forums.powershell.org/t/understanding-mocking/10125 "2018-02-14T15:36:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dkbrown8](https://avatars.discourse-cdn.com/v4/letter/d/e274bd/32.png) [@dkbrown8](https://forums.powershell.org/u/dkbrown8)\
**Post date:** [February 14, 2018, 3:36pm UTC](https://forums.powershell.org/t/understanding-mocking/10125/1 "2018-02-14T15:36:59Z")

</div>

I just started working Pester and Mocking and I can’t seem Mocking to work. I bought The Pester Book and I’ve been using the advice to create a Pester test for some functions that I am created to abstract a Credential store. I’m abstracting the Credential store because we are using Windows Credential store in a tactical deployment and long term we will be moving to a Vault based credential store.

One of the functions retrieve a credential from the store. The function currently uses the Get-StoredCredential from the CredentialManager module to retrieve the credential. So, for the Pester test, I was thinking of using Mock to mock this command to return a PSCredential object as appose to actually retrieve a credential from the credential store.

```
Function Get-ServerCredential {
    
    Param (
        [Parameter(Mandatory=$True,Position=0)]
        [string]$serverFQDN,
        [Parameter(Mandatory=$True,Position=1)]
        [string]$serverAccount
    )#End Param
    Import-Module CredentialManager -Force

    $storeURL = ($serverAccount -replace ('@','.')) + '.' + $serverFQDN
    $storedCred = Get-StoredCredential -Target $storeURL
    if ($storedCred){
        Write-Output $storedCred 
    } else {
        throw "Invalid Account"
    }
}
```

The Pester test verifies that a PSCredential Object is returned, but every time I run the Pester test, it doesn’t appear to be using the Mock and I get an error message stating the the test failed. Here is the Pester test file

```
$Here = "$(Split-Path -parent $MyInvocation.MyCommand.Path)"
$ModuleName = Get-ChildItem "$Here\..\*.psd1"
Import-Module $ModuleName -force
Import-Module CredentialManager -force

$testCases = @( @{AccountName = 'root'})
Describe 'Get-ServerCredential when it''s an valid account'{
    mock Get-StoredCredential -MockWith {New-MockObject -Type 'System.Management.Automation.PSCredential'}
    It "Should Return a PSCredential Object" -TestCases $testCases {
        param($AccountName)
        Get-ServerCredential -serverFQDN "some server FQDN" -serverAccount $AccountName|Should BeOfType PSCredential
    }

}
```

I banging my head over this. I have change the Mock several different times. I’ve use a “return New-Object -Type ‘System.Management.Automation.PSCredential’” to MockWith. I seem to be missing something. Does anybody see something with this code that would be preventing me from using Mock??

---

<div class="post-metadata">

**Author:** ![adbertram](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/adbertram/32/193_2.png) [@adbertram](https://forums.powershell.org/u/adbertram)\
**Post date:** [February 14, 2018, 7:00pm UTC](https://forums.powershell.org/t/understanding-mocking/10125/2 "2018-02-14T19:00:42Z")

</div>

Does Get-StoredCredential use Get-Credential by any chance? When you mock something, it doesn’t just create a blank copy of the command, it uses a proxy command which includes all of the parameters. If memory serves me correctly, Get-Credential is something that can’t be mocked easily. Check out the Mocking the Unmockable chapter in the Pester Book. It talks about this in depth.

---

<div class="post-metadata">

**Author:** ![dkbrown8](https://avatars.discourse-cdn.com/v4/letter/d/e274bd/32.png) [@dkbrown8](https://forums.powershell.org/u/dkbrown8)\
**Post date:** [February 15, 2018, 9:27am UTC](https://forums.powershell.org/t/understanding-mocking/10125/3 "2018-02-15T09:27:26Z")

</div>

I can’t tell. The [CredentialManager](https://www.powershellgallery.com/packages/CredentialManager/2.0) Module is binary module that does return a PSCredential object when you make that call. I suspect that since the module is a binary, that Mock might not work for that. I am going back over that chapter to see if something clicks for me.

---

<div class="post-metadata">

**Author:** ![dkbrown8](https://avatars.discourse-cdn.com/v4/letter/d/e274bd/32.png) [@dkbrown8](https://forums.powershell.org/u/dkbrown8)\
**Post date:** [February 15, 2018, 12:52pm UTC](https://forums.powershell.org/t/understanding-mocking/10125/4 "2018-02-15T12:52:41Z")

</div>

Wrapping the Get-StoredCredential worked. I was able Mock the wrapper function

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 9:13pm UTC](https://forums.powershell.org/t/understanding-mocking/10125/5 "2024-05-16T21:13:31Z")

</div>

Closed on May 16, 2024, 9:13pm UTC


