This tests is written to show a pattern of how iteration will work in Pester 5.1 (as of Oct 23, 2020 is still in beta).
The File Integrity test source came from one of Chrissy LeMaire's module kbupdate. You can find the source version here.
The function included in this test, Get-FileEncoding
, was sourced from Frank Richard's post.
The ForEach
can be used with the Describe
and Context
blocks in your tests. The Describe
block you would execute code prior in order to provide the object (as I did for $allFiles
). The Context
is a bit different because a major change in Pester 5 was code that is to be run prior to a Context
or It
block has to be done within a BeforeAll
or BeforeEach
block.
When you iterate over the objects and you want to reference the object in the description of the Describe
, Context
or It
block you can use <_>
. Within code such as BeforeAll
you would use the same $_
that we use in PowerShell piping. I use the later for setting variables in the BeforeAll
block such as $name
and $fullName
.
A major thing ot remember when you are converting your scripts, or writing new ones for Pester 5, is the syntax for Should statement is no being enforced. Previously we could just do $true | Should Be $true
but that will now error with the following:
RuntimeException: Legacy Should syntax (without dashes) is not supported in Pester 5. Please refer to migration guide at: http://pester.dev/docs/migrations/v3-to-v4
Instead you must use proper parameter syntax as $true | Should -Be $true
(notice the dash).