Pester Mocking a script that varies output

Welcome Forums General PowerShell Q&A Pester Mocking a script that varies output

Viewing 19 reply threads
  • Author
    Posts
    • #50725
      Participant
      Topics: 9
      Replies: 16
      Points: 0
      Rank: Member

      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

    • #50730
      Moderator
      Topics: 2
      Replies: 525
      Points: 24
      Team Member
      Rank: Member

      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.

      • #50734
        Participant
        Topics: 9
        Replies: 16
        Points: 0
        Rank: Member

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

    • #50740
      Moderator
      Topics: 2
      Replies: 525
      Points: 24
      Team Member
      Rank: Member

      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.

      • #50753
        Participant
        Topics: 9
        Replies: 16
        Points: 0
        Rank: Member

        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

    • #50755
      Moderator
      Topics: 2
      Replies: 525
      Points: 24
      Team Member
      Rank: Member

      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)"
          }
      }
      
    • #50757
      Participant
      Topics: 9
      Replies: 16
      Points: 0
      Rank: Member

      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'
          }
      }
    • #50759
      Participant
      Topics: 9
      Replies: 16
      Points: 0
      Rank: Member

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

    • #50773
      Moderator
      Topics: 2
      Replies: 525
      Points: 24
      Team Member
      Rank: Member

      Example (mock function without return value):

      Mock My-Function -MockWith {}
      
    • #50775
      Moderator
      Topics: 2
      Replies: 525
      Points: 24
      Team Member
      Rank: Member

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

    • #50777
      Participant
      Topics: 9
      Replies: 16
      Points: 0
      Rank: Member

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

    • #50779
      Participant
      Topics: 5
      Replies: 39
      Points: 0
      Rank: Member

      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 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.

    • #50781
      Participant
      Topics: 9
      Replies: 16
      Points: 0
      Rank: Member

      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

    • #50783
      Moderator
      Topics: 2
      Replies: 525
      Points: 24
      Team Member
      Rank: Member

      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.

    • #50785
      Participant
      Topics: 9
      Replies: 16
      Points: 0
      Rank: Member

      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.

    • #50787
      Participant
      Topics: 9
      Replies: 16
      Points: 0
      Rank: Member

      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.

    • #50791
      Participant
      Topics: 5
      Replies: 39
      Points: 0
      Rank: Member

      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'
          }
      }
    • #50796
      Participant
      Topics: 9
      Replies: 16
      Points: 0
      Rank: Member

      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?

    • #50799
      Participant
      Topics: 5
      Replies: 39
      Points: 0
      Rank: Member

      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) + '*')
          }
      }
    • #50803
      Member
      Topics: 9
      Replies: 2322
      Points: 0
      Rank: Member

      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'
              }
          }
      }
      
    • #50817
      Participant
      Topics: 9
      Replies: 16
      Points: 0
      Rank: Member

      Hi Anthony, HKLM:\Software\WOW6432Node\This is Testing this will get changed when I run the same in 32 bit so how can I validate the test case

    • #50905
      Participant
      Topics: 5
      Replies: 39
      Points: 0
      Rank: Member

      Well first off in your function I would be checking for the OS version. If it's 32 bit write the 32 bit registry and if it's 64bit write the 64bit registry. In your function you could use

      (Get-WmiObject Win32_OperatingSystem).OSArchitecture

      to determine the OS version then mock Get-WmiObject to either return 32-bit or 64-bit

Viewing 19 reply threads
  • The topic ‘Pester Mocking a script that varies output’ is closed to new replies.