# Pester Mocking a script that varies output

**URL:** <https://forums.powershell.org/t/pester-mocking-a-script-that-varies-output/7005>\
**Category:** PowerShell Help\
**Created:** [August 17, 2016, 2:30am UTC](https://forums.powershell.org/t/pester-mocking-a-script-that-varies-output/7005 "2016-08-17T02:30:21Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![krishna-chaitanya](https://avatars.discourse-cdn.com/v4/letter/k/e5b9ba/32.png) [@krishna-chaitanya](https://forums.powershell.org/u/krishna-chaitanya)\
**Post date:** [August 17, 2016, 2:30am UTC](https://forums.powershell.org/t/pester-mocking-a-script-that-varies-output/7005/1 "2016-08-17T02:30:21Z")

</div>

Hi all I have written a script to display the current user info, I would like to write Pester test case that should mock the output, also if I don’t have return in the function how can I write a test for that too

```
function Get-CurrentUserInfo
{

    $domain = [Environment]::UserDomainName
    $user = [Environment]::UserName
    if (!([string]::IsNullOrEmpty($domain))) { $domain = $domain + '\' }

    $currentUser = $domain + $user

    #return $currentUser I have commented out so that it will not return any output
}
```

Here is my test case when there is return

```
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
. "$here\Get-CurrentUserInfo.ps1"
Describe "CurrentUser" {
    It "CurrentUser Info" {
        Get-CurrentUserInfo | Should be 'MY-PC\username'
    }
}
```

Which works fine with my PC but when I execute the same in other PC it will fail so how can I make it unique

---

<div class="post-metadata">

**Author:** ![daniel-krebs](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/daniel-krebs/32/484_2.png) [@daniel-krebs](https://forums.powershell.org/u/daniel-krebs)\
**Post date:** [August 17, 2016, 2:59am UTC](https://forums.powershell.org/t/pester-mocking-a-script-that-varies-output/7005/2 "2016-08-17T02:59:20Z")

</div>

I would change the Should be ‘MY-PC\username’ to Should be “$($env:ComputerName)$($env:UserName)” in your Pester test. That should get you around mocking the function in the first place.

---

<div class="post-metadata">

**Author:** ![krishna-chaitanya](https://avatars.discourse-cdn.com/v4/letter/k/e5b9ba/32.png) [@krishna-chaitanya](https://forums.powershell.org/u/krishna-chaitanya)\
**Post date:** [August 17, 2016, 3:21am UTC](https://forums.powershell.org/t/pester-mocking-a-script-that-varies-output/7005/3 "2016-08-17T03:21:00Z")

</div>

Hi Daniel can you give me the script I was just confused where to Mock it exactly

---

<div class="post-metadata">

**Author:** ![daniel-krebs](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/daniel-krebs/32/484_2.png) [@daniel-krebs](https://forums.powershell.org/u/daniel-krebs)\
**Post date:** [August 17, 2016, 4:16am UTC](https://forums.powershell.org/t/pester-mocking-a-script-that-varies-output/7005/4 "2016-08-17T04:16:17Z")

</div>

For the example you’ve provided you don’t really need to use mocking because you can just test for the correct value. If you mock a function you’re not testing it because the mock function will be invoked instead.

Example 1 (just test for the expected value)

```
function Get-CurrentUserInfo
{

    $domain = [Environment]::UserDomainName
    $user = [Environment]::UserName
    if (!([string]::IsNullOrEmpty($domain))) { $domain = $domain + '\' }

    $currentUser = $domain + $user

    return $currentUser
}
```

```
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
. "$here\Get-CurrentUserInfo.ps1"

Describe "CurrentUser" {
    It "CurrentUser Info" {
        Get-CurrentUserInfo | Should be "$($env:UserDomainName)\$($env:UserName)"
    }
}
```

Example 2 (Get-CurrentUserInfo replaced by Mock function):

```
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
. "$here\Get-CurrentUserInfo.ps1"

Describe "CurrentUser" {
    It "CurrentUser Info" {
        Mock Get-CurrentUserInfo -MockWith { return 'Hello PowerShell' }
        Get-CurrentUserInfo | Should be 'Hello PowerShell'
    }
}
```

Your “Should Be” test doesn’t need to reflect the actual output of the function because you can’t just fake it because your original function is never being invoked.

I hope above makes sense. If not, please let us know.

---

<div class="post-metadata">

**Author:** ![krishna-chaitanya](https://avatars.discourse-cdn.com/v4/letter/k/e5b9ba/32.png) [@krishna-chaitanya](https://forums.powershell.org/u/krishna-chaitanya)\
**Post date:** [August 17, 2016, 4:44am UTC](https://forums.powershell.org/t/pester-mocking-a-script-that-varies-output/7005/5 "2016-08-17T04:44:59Z")

</div>

Hi Daniel thanks for the code, but I am not able to debug my main function to see whats actually happening. If I save the file as .psm1 instead of .ps1 will the logic remains same or will it change

---

<div class="post-metadata">

**Author:** ![daniel-krebs](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/daniel-krebs/32/484_2.png) [@daniel-krebs](https://forums.powershell.org/u/daniel-krebs)\
**Post date:** [August 17, 2016, 4:55am UTC](https://forums.powershell.org/t/pester-mocking-a-script-that-varies-output/7005/6 "2016-08-17T04:55:14Z")

</div>

Only the 2nd line of the test script changes to import your module into Pester session instead of dot-sourcing it.

```
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
Import-Module "$here\Get-CurrentUserInfo.psm1" -Force

Describe "CurrentUser" {
    It "CurrentUser Info" {
        Get-CurrentUserInfo | Should be "$($env:UserDomainName)\$($env:UserName)"
    }
}
```

---

<div class="post-metadata">

**Author:** ![krishna-chaitanya](https://avatars.discourse-cdn.com/v4/letter/k/e5b9ba/32.png) [@krishna-chaitanya](https://forums.powershell.org/u/krishna-chaitanya)\
**Post date:** [August 17, 2016, 4:59am UTC](https://forums.powershell.org/t/pester-mocking-a-script-that-varies-output/7005/7 "2016-08-17T04:59:18Z")

</div>

That thing I got but coming to mocking will it be same?

```
Import-Module "D:\MyFile.psm1" -Force
Describe "My Test" {
    It "My Test" {
        Mock My-Function -MockWith { return 'Hello' }
        My-Function | Should be 'Hello'
    }
}
```

---

<div class="post-metadata">

**Author:** ![krishna-chaitanya](https://avatars.discourse-cdn.com/v4/letter/k/e5b9ba/32.png) [@krishna-chaitanya](https://forums.powershell.org/u/krishna-chaitanya)\
**Post date:** [August 17, 2016, 5:02am UTC](https://forums.powershell.org/t/pester-mocking-a-script-that-varies-output/7005/8 "2016-08-17T05:02:33Z")

</div>

Also if my function is not returning any value how can I mock them

---

<div class="post-metadata">

**Author:** ![daniel-krebs](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/daniel-krebs/32/484_2.png) [@daniel-krebs](https://forums.powershell.org/u/daniel-krebs)\
**Post date:** [August 17, 2016, 5:18am UTC](https://forums.powershell.org/t/pester-mocking-a-script-that-varies-output/7005/9 "2016-08-17T05:18:53Z")

</div>

Example (mock function without return value):

```
Mock My-Function -MockWith {}
```

---

<div class="post-metadata">

**Author:** ![daniel-krebs](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/daniel-krebs/32/484_2.png) [@daniel-krebs](https://forums.powershell.org/u/daniel-krebs)\
**Post date:** [August 17, 2016, 5:20am UTC](https://forums.powershell.org/t/pester-mocking-a-script-that-varies-output/7005/10 "2016-08-17T05:20:16Z")

</div>

To answer your other question. Yes, mocking will be the same if you import a module instead of dot-sourcing a script.

---

<div class="post-metadata">

**Author:** ![krishna-chaitanya](https://avatars.discourse-cdn.com/v4/letter/k/e5b9ba/32.png) [@krishna-chaitanya](https://forums.powershell.org/u/krishna-chaitanya)\
**Post date:** [August 17, 2016, 5:22am UTC](https://forums.powershell.org/t/pester-mocking-a-script-that-varies-output/7005/11 "2016-08-17T05:22:06Z")

</div>

Thanks Daniel but when I include Mock I am not able to hit the breakpoints to my actual solution any problem?

---

<div class="post-metadata">

**Author:** ![theonlyway](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/theonlyway/32/395_2.png) [@theonlyway](https://forums.powershell.org/u/theonlyway)\
**Post date:** [August 17, 2016, 5:24am UTC](https://forums.powershell.org/t/pester-mocking-a-script-that-varies-output/7005/12 "2016-08-17T05:24:57Z")

</div>

Well no value returned should basically be a NULL so I would assume the following:

```
Describe "My Test" {
    It "My Test" {
        Mock My-Function -MockWith { $null }
        My-Function | Should be $null
    }
}
```

Depending on how your module is written though you may need to look in to using inmodulescope [InModuleScope · pester/Pester Wiki · GitHub](https://github.com/pester/Pester/wiki/InModuleScope) but for the most part mocking with a simple function being imported like that will work the same.

---

<div class="post-metadata">

**Author:** ![krishna-chaitanya](https://avatars.discourse-cdn.com/v4/letter/k/e5b9ba/32.png) [@krishna-chaitanya](https://forums.powershell.org/u/krishna-chaitanya)\
**Post date:** [August 17, 2016, 5:46am UTC](https://forums.powershell.org/t/pester-mocking-a-script-that-varies-output/7005/13 "2016-08-17T05:46:43Z")

</div>

Hi Anthony when I am using Mock, I am unable to hit the break point in my main module. I would like to view what my function is returning

---

<div class="post-metadata">

**Author:** ![daniel-krebs](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/daniel-krebs/32/484_2.png) [@daniel-krebs](https://forums.powershell.org/u/daniel-krebs)\
**Post date:** [August 17, 2016, 6:08am UTC](https://forums.powershell.org/t/pester-mocking-a-script-that-varies-output/7005/14 "2016-08-17T06:08:20Z")

</div>

Krishna, that is correct. If you’ve mocked the function you’re trying to test it will never be called.

One wouldn’t mock the function to be tested but any function being called by the function under test because you can’t provide the correct environment or access to an external resource like a database or API.

---

<div class="post-metadata">

**Author:** ![krishna-chaitanya](https://avatars.discourse-cdn.com/v4/letter/k/e5b9ba/32.png) [@krishna-chaitanya](https://forums.powershell.org/u/krishna-chaitanya)\
**Post date:** [August 17, 2016, 6:16am UTC](https://forums.powershell.org/t/pester-mocking-a-script-that-varies-output/7005/15 "2016-08-17T06:16:45Z")

</div>

OK I have a script which creates a registry if it is 32 bit it will return respective path if not it will 64 bit path. I mocked that one and when I execute it I am not able to see the registry getting created.

---

<div class="post-metadata">

**Author:** ![krishna-chaitanya](https://avatars.discourse-cdn.com/v4/letter/k/e5b9ba/32.png) [@krishna-chaitanya](https://forums.powershell.org/u/krishna-chaitanya)\
**Post date:** [August 17, 2016, 6:20am UTC](https://forums.powershell.org/t/pester-mocking-a-script-that-varies-output/7005/16 "2016-08-17T06:20:34Z")

</div>

This is my sample script which will append a text to the given path

.psm1

```
Function Test-writeHost
{
    Add-Content 'C:\duplicates.txt' 'The Message'
}
```

My test file

Import-Module “C:\MyScripts\Test-writeHost.psm1” -Force

```
Describe "Test write" {
    It "Test write" {
        Mock Test-writeHost -MockWith { return $null }
        Test-writeHost | Should be $null
    }
}
```

When I run this test the content is not getting appended to text file.

---

<div class="post-metadata">

**Author:** ![theonlyway](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/theonlyway/32/395_2.png) [@theonlyway](https://forums.powershell.org/u/theonlyway)\
**Post date:** [August 17, 2016, 6:30am UTC](https://forums.powershell.org/t/pester-mocking-a-script-that-varies-output/7005/17 "2016-08-17T06:30:00Z")

</div>

If you need to write something to a file for a test then you don’t really need to mock it. You just do it. You generally use mock to return data you know will test your logic.

```
Function Test-writeHost
{
    Add-Content 'C:\TEMP\duplicates.txt' 'The Message'
}

Describe "Test write" {
    It "Test write" {
    Test-writeHost
        Get-Content -Path 'C:\TEMP\duplicates.txt' | Should be 'The Message'
    }
}
```

For instance you’d mock the get-content command to return a different output if you wanted to test for a failure or something.

```
Function Test-writeHost
{
    Add-Content 'C:\TEMP\duplicates.txt' 'The Message'
}

Describe "Test write" {
    It "Test write" {
      Test-writeHost
      Mock Get-Content -MockWith { 'Clearly not the message' }
        Get-Content -Path 'C:\TEMP\duplicates.txt' | Should be 'Clearly not the message'
    }
}
```

---

<div class="post-metadata">

**Author:** ![krishna-chaitanya](https://avatars.discourse-cdn.com/v4/letter/k/e5b9ba/32.png) [@krishna-chaitanya](https://forums.powershell.org/u/krishna-chaitanya)\
**Post date:** [August 17, 2016, 6:47am UTC](https://forums.powershell.org/t/pester-mocking-a-script-that-varies-output/7005/18 "2016-08-17T06:47:49Z")

</div>

Ok if I understand correctly

```
function CreateRegistry
{
    if ([System.IntPtr]::Size -eq 4)
    {
       [string]$RegisryPath = "HKLM:\Software\This is Testing"
    }
    else
    {
         [string]$RegisryPath = "HKLM:\Software\WOW6432Node\This is Testing"
    }

    if (!(Test-Path -Path $RegisryPath))
    {
       $reg = $RegisryPath
       New-Item $reg -ItemType Key -Force
       Set-ItemProperty -Path $reg -Name CreatedDateTime -Value (Get-Date).ToString()
    }

   return [string]$RegisryPath 
}
```

If I run the script I am getting the path as H **KLM:\Software\WOW6432Node\This is Testing**

To mock this I have written as follows

```
Import-Module "C:\CreateRegistry.psm1" -Force
Describe "RegistryPath" {
    CreateRegistry
    It "Creates Registry path" {        
        Mock CreateRegistry -MockWith { return 'HKLM:\Software\This is Testing' }
        CreateRegistry Should be 'HKLM:\Software\This is Testing'
    }
}
```

Am I correct?

---

<div class="post-metadata">

**Author:** ![theonlyway](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/theonlyway/32/395_2.png) [@theonlyway](https://forums.powershell.org/u/theonlyway)\
**Post date:** [August 17, 2016, 7:14am UTC](https://forums.powershell.org/t/pester-mocking-a-script-that-varies-output/7005/19 "2016-08-17T07:14:54Z")

</div>

Somewhat. The question you need to ask yourself is do you want to actually test if your function works or do you want to test if the logic of your function works? Generally what you want to do with Pester is test all of it which means you generally end up with multiple tests for the same function just doing different things.

You probably want to test your logic of your function first by mocking. Then assuming your function is doing something easy to reverse like creating a registry key after your mock tests have passed the your “logic tests” you can then just let the function go off and actually run then test to see if it actually created what you expected it to.

As an example for your particular example I would do something like this

```
Import-Module "C:\Temp\CreateRegistry.psm1" -Force
Describe "RegistryPath" {
    CreateRegistry
    It "Creates Registry path" {        
        (Get-ItemProperty -Path 'HKLM:\Software\WOW6432Node\This is Testing').CreatedDateTime | Should belike ((Get-Date -Format dd/MM/yyyy) + '*')
    }
}
```

However if you wanted to mock it I would do something like this

```
Import-Module "C:\Temp\CreateRegistry.psm1" -Force
  $Key = New-Object -TypeName PSObject -Property @{
  CreatedDateTime = (Get-Date -Format dd/MM/yyyy)
  }
Describe "RegistryPath" {
    It "Creates Registry path" { 
    Mock Get-ItemProperty -MockWith { $Key }       
        (Get-ItemProperty -Path 'HKLM:\Software\WOW6432Node\This is Testing').CreatedDateTime | Should belike ((Get-Date -Format dd/MM/yyyy) + '*')
    }
}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [August 17, 2016, 7:46am UTC](https://forums.powershell.org/t/pester-mocking-a-script-that-varies-output/7005/20 "2016-08-17T07:46:36Z")

</div>

There’s nothing in your current function that can be mocked, and mocking the whole function itself is pointless. (Your test would basically be saying, at that point, “did I mock the function?”)

What you can do is wrap your calls to the static members of the Environment class in PowerShell functions, then mock _those_ functions. At that point, you will be performing useful tests of your function’s logic (such as does it inject the backslash properly if a domain exists, does it return a value at all, etc.) That code could look like this:

```
function Get-CurrentUserInfo
{

    $domain = Get-DomainName
    $user = Get-UserName
    if (!([string]::IsNullOrEmpty($domain))) { $domain = $domain + '\' }

    $currentUser = $domain + $user

    #return $currentUser I have commented out so that it will not return any output
}

function Get-UserName
{
    return [Environment]::UserName
}

function Get-DomainName
{
    return [Environment]::UserDomainName
}
```

Now your tests can look like this:

```
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
. "$here\Get-CurrentUserInfo.ps1"

Describe "CurrentUser" {
    Mock Get-UserName { return 'MockUser' }

    Context 'When there is no domain name' {
        Mock Get-DomainName

        It "Returns the proper value" {
            Get-CurrentUserInfo | Should be 'MockUser'
        }
    }

    Context 'When there is a domain name' {
        Mock Get-DomainName { return 'MockDomain' }

        It "Returns the proper value" {
            Get-CurrentUserInfo | Should be 'MockDomain\MockUser'
        }
    }
}
```

[Next page](https://forums.powershell.org/t/pester-mocking-a-script-that-varies-output/7005.md?page=2)
