-
I have a script that does the heavy lifting in the A quick example where we want to validate the integrity of a parent-child lookup: $sb = {
BeforeDiscovery {
$discoveryLookups = @(
@{Id = 1; ParentId = '1' } # Root node
@{Id = 2; ParentId = '1' }
@{Id = 3; ParentId = '2' }
@{Id = 4; ParentId = '1000' } # Invalid Parent
)
}
Describe 'Check Lookups' {
Context 'Validate entries' -ForEach $discoveryLookups {
It 'has an ID field' {
$_.Id | Should -Not -BeNullOrEmpty
}
It 'has a ParentID field' {
$_.ParentId | Should -Not -BeNullOrEmpty
}
}
## This is the test under review
Context 'Validate Parent Lookups' {
It 'has a Parent ID that refers to an existing ID' -ForEach $discoveryLookups {
$entry = $_
$entry.ParentId | Should -BeIn $discoveryLookups.Id -Because "Parent Id ${entry.ParentId} should exist as an Id"
}
}
}
}
$configuration = New-PesterConfiguration -Hashtable @{
Output = @{ Verbosity = 'Detailed'; StackTraceVerbosity = 'None' };
Run = @{ Container = New-PesterContainer -ScriptBlock $sb }
}
Invoke-Pester -Configuration $configuration In the second context block, I would need to refer to the One approach I've taken is to create a wrapper parameter, so it can be passed directly without causing an iteration, like so:# BeforeDiscovery {
...
### Wrap the data in a single-element array - more could be added if required.
$wrapper = @(@{Data = $discoveryLookups})
}
Describe 'Check Lookups' {
...
### Added $wrapper with 1 value, so it doesn't interact
Context 'Validate Parent Lookups' -ForEach $wrapper {
BeforeAll {
# Store .Data at the context level
$contextLookups = $_.Data
}
### Still lookup over all the elements in the $discoveryLookups
It 'has a Parent ID that refers to an existing ID' -ForEach $discoveryLookups {
$entry = $_
$entry.ParentId | Should -BeIn $contextLookups.Id -Because "Parent Id $($entry.ParentId) should relate to another Id"
}
}
} Huzzah and horrah this works as expected: Starting discovery in 1 files.
Discovery found 12 tests in 11ms.
Running tests.
Describing Check Lookups
...
Context Validate Parent Lookups
[+] has a Parent ID that refers to an existing ID 3ms (1ms|2ms)
[+] has a Parent ID that refers to an existing ID 2ms (1ms|1ms)
[+] has a Parent ID that refers to an existing ID 1ms (1ms|1ms)
[-] has a Parent ID that refers to an existing ID 15ms (15ms|1ms)
Expected collection @(1, 2, 3, 4) to contain '1000', because Parent Id 1000 should relate to another Id, but it was not found.
Tests completed in 319ms
Tests Passed: 11, Failed: 1, Skipped: 0, Inconclusive: 0, NotRun: 0 My question is - is this the approach to take for global variables, or is there an alternative to consider? Many thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Yes. Using a wrapper dictionary through
The alternative is script parameters ( |
Beta Was this translation helpful? Give feedback.
Yes. Using a wrapper dictionary through
-ForEach
is the recommended approach. Tips:Describe 'Check Lookups' -ForEach @{ DiscoveryLookups = $discoveryLookups }
BeforeAll
reassigning$_.Data
and just use$DiscoveryLookups
in Run-phase. Using capital key/variable letter to illustrate.The alternative is script parameters (
params($MyParam = 'default data')
). They're automatically included in Run as well by Pester as container (file) level Data. It's typically used for providing external data when invoking Pester - but it works with default valu…