Skip to content

Instantly share code, notes, and snippets.

@petervandivier
Last active July 7, 2022 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petervandivier/794fc652539e8834839f9629c512898a to your computer and use it in GitHub Desktop.
Save petervandivier/794fc652539e8834839f9629c512898a to your computer and use it in GitHub Desktop.
Pester PS Class

I'm attempting (and failing) to run a data-driven Pester v5 test over a PS Class. In the Tests/ directory are the 3 (currently failing) implemetations.

using namespace System.Management.Automation
class foo : IValidateSetValuesGenerator {
[string[]] GetValidValues() {
$Values = @(
'foo'
'bar'
'baz'
)
return $Values
}
}
# class fooCollection {
# [foo[]]$Collection
# }
@{
RootModule = '.\foo.psm1'
ModuleVersion = '0.0'
VariablesToExport = @(
'FooHash'
)
FunctionsToExport = @(
'Get-Foo'
)
ScriptsToProcess = @(
'Classes/classes.ps1'
)
}
Push-Location $PSScriptRoot
. ./Functions/Get-Foo.ps1
$global:FooHash = @{
foo = @{}
bar = @{}
baz = @{}
}
Export-ModuleMember -Variable FooHash
Pop-Location
function Get-Foo {
[CmdletBinding()]
Param(
[Parameter(Mandatory)]
[ValidateSet([foo])]
[string]$Name
)
$Name
}
#Requires -Modules foo
Push-Location $PSScriptRoot
BeforeAll {
. ./../Classes/foo.ps1
}
Describe "FooHash should contain all of type [foo]" {
It "<_>" -ForEach ($FooHash.GetEnumerator().Name | Sort-Object) {
$_ | Should -BeIn ([foo]::New().GetValidValues())
}
}
Describe "[foo] should be fully enumerated in `$FooHash" {
It "<_>" -ForEach ([foo]::New().GetValidValues() | Sort-Object) {
$FooHash.GetEnumerator().Name | Should -Contain $_
}
}
Pop-Location
#Requires -Modules foo
Push-Location $PSScriptRoot
BeforeAll {
. ./../Classes/foo.ps1
}
Describe "FooHash should contain all of type [foo]" {
It "<_>" -ForEach ($FooHash.GetEnumerator().Name | Sort-Object) {
$_ | Should -BeIn ([foo]::New().GetValidValues())
}
}
Describe "[foo] should be fully enumerated in `$FooHash" {
It "<_>" -ForEach ([foo]::New().GetValidValues() | Sort-Object) {
$FooHash.GetEnumerator().Name | Should -Contain $_
}
}
Pop-Location
#Requires -Modules foo
BeforeAll {
$module = @{
ModuleName = 'foo'
}
}
InModuleScope @module {
Describe "FooHash should contain all of type [foo]" {
It "<_>" -ForEach ($FooHash.GetEnumerator().Name | Sort-Object) {
$_ | Should -BeIn ([foo]::New().GetValidValues())
}
}
Describe "[foo] should be fully enumerated in `$FooHash" {
It "<_>" -ForEach ([foo]::New().GetValidValues() | Sort-Object) {
$FooHash.GetEnumerator().Name | Should -Contain $_
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment