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