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:

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.

help Should
function Should {
SupportsShouldProcess = $false
ConfirmImpact = 'Low'
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[Parameter(Mandatory = $false, Position = 0)]
[Parameter(Mandatory = $false, Position = 1)]
[Parameter(Mandatory = $false, Position = 2)]
[Parameter(Mandatory = $false, Position = 3)]
[Parameter(Mandatory = $false, Position = 4)]
[Parameter(Mandatory = $false, Position = 5)]
$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 '';
# 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
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.

view raw


hosted with ❤ by GitHub

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.