Failed Pester Test

Tagged: ,

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Adam Bertram Adam Bertram 2 months, 2 weeks ago.

  • Author
    Posts
  • #68280
    Profile photo of Michael
    Michael
    Participant

    Hi Everyone!

    I have a Pester test setup to test out one function within a .psm1 file. The function is very simple and pretty much adds to an existing cmdlet so that the PS code is more clean.

    function Get-Datetime {
        Get-Date -Format "MM.dd.yyyy:hh.mm.ss"
    }
    
    function Add-RabbitMQContent {
        param
        (
        [string] $Path = "C:\predefinedlocation",
        [Parameter(Mandatory=$true)] [string] $Value
        )
    
        Add-Content -Path $Path -Value "$(Get-DateTime) $Value"
    }
    
    #Example of when I am using the function. I like that I do not have to specify the location, but I can if I choose to and the #value will also write out the date time next to it like a log file. 
    Add-RabbitMQContent -Value "Exiting program!"
    

    My Pester Test fails at using the "Add-RabbitMQContent" function noted in code below. If I attempt to pass a local directory or use the $TestDrive\ location it does not work. Any thoughts on why?

    $here = Split-Path -Parent $MyInvocation.MyCommand.Path
    $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
    . "$here\$sut"
    
    Import-Module "C:\LocationtoModule\RabbitMQ-PartitionCheck.psm1"
    Describe "RabbitMQ-PartitionCheck Module" {
        InModuleScope -ModuleName RabbitMQ-PartitionCheck {
            Context "Checking Get-DateTime Function" {
                $DateTime = Get-DateTime
                It "Verifying Get-Date Output Format MM.dd.yyyy:hh.mm.ss" {
                    $DateTime | Should Match "\d\d\.\d\d\.\d\d\d\d\:\d\d\.\d\d\.\d\d"
                }
                It "Get-Date should Not be NULL" {
                    $DateTime | Should Not Be $NULL
                }
            }
            Context "Checking Add-RabbitMQContent Function" {
                It 'TestDrive should exist' {
                    $TestDrive | Should Exist
                }
         ###################### Fails at below test ######################################       
                It 'Adding Test.txt to TestDrive' {
                    Setup -File "test.txt"
                    $TestPath = "$TestDrive\test.txt"
                    Add-RabbitMQContent -Path "$TestPath" -Value "Hi"
                    $result = Get-Content $TestPath
                    $result | Should be "Hi"
                }
            }
        }
    }
    
  • #68316
    Profile photo of Adam Bertram
    Adam Bertram
    Moderator

    What is the Setup function inside of your test? Also, the reason it's failing is because you're writing

    "$(Get-DateTime) $Value"

    to the file but your test is only checking for

    $Value

    . Your test should look something like this:

    $now =  Get-Date -Format 'MM.dd.yyyy:hh.mm.ss'
    It 'Adding Test.txt to TestDrive' {
        $TestPath = "$TestDrive\test.txt"
        Add-RabbitMQContent -Path $TestPath -Value "Hi"
        $result = Get-Content $TestPath
        $result | Should belike "$now* Hi"
    }

    It's going to be hard to test to ensure the time was inserted correctly though since it may not be the same second.

  • #68320
    Profile photo of Michael
    Michael
    Participant

    Adam,

    The Setup is not a self-made function. It is a cmdlet to add files to your TestDrive. So I cannot paste URL link in this post but here is code you can find in the Pester project on Github using "Setup."

    1 Set-StrictMode -Version Latest 
    2 
     
    3 InModuleScope Pester { 
    4     Describe "PesterContain" { 
    5         Context "when testing file contents" { 
    ############ Below is the Setup being used.  ############################
    6             Setup -File "test.txt" "this is line 1`nrush is awesome`nAnd this is Unicode: " 
    7 
     
    8             It "returns true if the file contains the specified content" { 
    9                 "$TestDrive\test.txt" | Should Contain rush 
    10                 "$TestDrive\test.txt" | Should -Contain rush 
    11             } 
    12 
     
    13             It "returns true if the file contains the specified content with different case" { 
    14                 "$TestDrive\test.txt" | Should Contain RUSH 
    15                 "$TestDrive\test.txt" | Should -Contain RUSH 
    16             } 
    17 
     
    18             It "returns false if the file does not contain the specified content" { 
    19                 "$TestDrive\test.txt" | Should Not Contain slime 
    20                 "$TestDrive\test.txt" | Should -Not -Contain slime 
    21             } 
    22 
     
    23             It "returns true if the file contains the specified UTF8 content" { 
    24                 "$TestDrive\test.txt" | Should Contain "" 
    25                 "$TestDrive\test.txt" | Should -Contain "" 
    26             } 
    27         } 
    28     } 
    29 }
    

    OMG! Such a simple solution. I have been looking at my Pester testing all day. I should have walked away for a bit. I did not catch that. Thank you, Adam. I just this to my test and it worked successfully.

    Thank you,
    -Michael

  • #68322
    Profile photo of Adam Bertram
    Adam Bertram
    Moderator

    It happens to the best of us. :). I was not aware of that setup command. I've just always created the file with Add-Content. Learn something new every day.

You must be logged in to reply to this topic.