This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ModuleName defines in which session state the mock will be effective, not in which module the function was defined. | |
# This is important when mocking functions exported from a module either when calling them from a script, or when calling | |
# them from a different module. In that case you want to define the mock in the place where the function is called from, | |
# not in the module where the function is defined. | |
# The only time you want to define the mock in the module where the function is defined is when you are testing an internal | |
# functions of the module (not shown here). | |
Invoke-Pester -Container ( | |
New-PesterContainer -ScriptBlock { | |
BeforeAll { | |
Get-Module m, n, o | Remove-Module |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Run only discovery, select tests that need really expensive setup that takes | |
# long time to be present in the target system. E.g. 30 minutes per file to be updated | |
# in a remote resource. And run just the discovery, setup all tests that would run across | |
# all files (we use only one here but it does not matter). | |
# And then run the tests, communicating back which resources were created so we get failures | |
# in their respective tests. | |
Import-Module Pester -RequiredVersion 5.2.0 -Force | |
$configuration = [PesterConfiguration]::Default |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
using System; | |
using System.Collections.Concurrent; | |
using System.Diagnostics; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Linq; | |
using System.Collections.Generic; | |
using System.IO; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Keeping a value from a mock call, or some other place | |
# while avoiding $script: to keep script scope clean | |
Describe "a" { | |
It 'Test' { | |
function Get-GitHubRelease ($Release) { } | |
# use a reference object instead of directly assiging to a | |
# variable to collect the values from a mock call | |
$container = @{} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Getting and setting variables from within a module into the calling scope | |
# make sure we re-import the module when experimenting with this | |
# to get our functions updated | |
Get-Module m | Remove-Module | |
New-Module m -ScriptBlock { | |
function Get-CallerVariable { | |
[CmdletBinding()] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-Filter ($Predicate) { | |
# gets the property hashtable of an object | |
$properties = $Predicate.PSObject.Properties | |
# runs the code below in a scriptblock to ensure that we only capture | |
# the desired $Properties variable in case we would have more variables | |
# in this function. This is not strictly necessary, but closure only captures | |
# local variables so it is useful trick to limit the variables that we capture. | |
& { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$m = Get-Command Invoke-Pester -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Module | |
$psv = $PSVersionTable.PSVersion | |
$pre = $m.PrivateData -and $m.PrivateData.PSData -and $m.PrivateData.PSData.PreRelease | |
$pv = if ($pre) { "$($m.Version)-$($m.PrivateData.PSData.PreRelease)" } else { $m.Version } | |
"Pester version : " + $pv + " " + $m.Path | |
"PowerShell version : " + $psv | |
"OS version : " + [System.Environment]::OSVersion.VersionString |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$harmony = "$PSScriptRoot/0Harmony.dll" | |
Import-Module $harmony | |
$Script:Patches = @() | |
function Set-StaticPropertyGetter { | |
param ( | |
[Parameter(Mandatory)] | |
[Type] $Type, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Describe "a" { | |
$Sources = @( | |
[PSCustomObject]@{ | |
Advanced = @{ | |
Enabled = $true | |
} | |
} | |
[PSCustomObject]@{ | |
Advanced = @{ | |
Enabled = $true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Adds BeforeAll at the top of Tests file to make it follow Pester v5 recommendation of putting | |
# all code into Pester controlled blocks. | |
# DO this: | |
# BeforeAll { | |
# . $PSScriptRoot/Code.ps1 | |
# } | |
# DO this: | |
# BeforeAll { |