Here is a small helper method that you can use when creating Pester tests and trying to run them in an interactive PowerShell session.
When executing statements that include the Pester Should
Cmdlet interactively in a PowerShell session you receive the following error message:
PS > $Host.Name | Should Be 'ConsoleHost' The Should command may only be used inside a Describe block. At C:\Program Files\WindowsPowerShell\Modules\Pester\Functions\Describe.ps1:125 char:9 + throw "The $CommandName command may only be used inside a Describe block ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (The Should comm...Describe block.:String) [], RuntimeException + FullyQualifiedErrorId : The Should command may only be used inside a Describe block.
With the Should
helper method below you can still execute the original Pester
statement without having to cut away the Pester part. It will instead show you the data type of the expression and its content along with the full Pester expression. So if you executed the same statement from above with the helper function loaded, you would receive the following output:
PS > $Host.Name | Should Be 'ConsoleHost' $Host.Name | Should Be 'ConsoleHost' Asserted type: 'System.String' Asserted content: ### ConsoleHost ###
All you have to do is to import the function in your PowerShell session. Keep in mind that if you later load the Pester module (either explicitly via Import-Module
or implicitly via using one of its Cmdlets) the original Pester Should
Cmdlet will shadow your helper definition. You would then have to import the script once more.
This file contains 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
help Should | |
function Should { | |
[CmdletBinding( | |
SupportsShouldProcess = $false | |
, | |
ConfirmImpact = 'Low' | |
)] | |
Param | |
( | |
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] | |
$InputObject | |
, | |
[Parameter(Mandatory = $false, Position = 0)] | |
$Arg0 | |
, | |
[Parameter(Mandatory = $false, Position = 1)] | |
$Arg1 | |
, | |
[Parameter(Mandatory = $false, Position = 2)] | |
$Arg2 | |
, | |
[Parameter(Mandatory = $false, Position = 3)] | |
$Arg3 | |
, | |
[Parameter(Mandatory = $false, Position = 4)] | |
$Arg4 | |
, | |
[Parameter(Mandatory = $false, Position = 5)] | |
$Arg5 | |
) | |
BEGIN | |
{ | |
} | |
PROCESS | |
{ | |
$callStack = Get-PSCallStack; | |
Write-Host –ForegroundColor Red –Object ($callStack[0].InvocationInfo.Line); | |
Write-Host ''; | |
Write-Host –ForegroundColor Yellow –Object ("Asserted type: '{0}'" -f $InputObject.GetType().FullName); | |
Write-Host ''; | |
Write-Host –ForegroundColor Gray –Object ("Asserted content:`r`n###`r`n{0}###" -f ($InputObject | Out-String)); | |
Write-Host ''; | |
} | |
END | |
{ | |
} | |
} | |
# | |
# Copyright 2015 d-fens GmbH | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
# |