Pester Mocking a script that varies output

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

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.

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

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.

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

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)"
    }
}

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'
    }
}

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

Example (mock function without return value):

Mock My-Function -MockWith {}

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

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

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 but for the most part mocking with a simple function being imported like that will work the same.

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

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.

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.

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.

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'
    }
}

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 HKLM:\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?

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) + '*')
    }
}

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'
        }
    }
}