Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing for mandatory parameters with Pester

Tags:

People also ask

How do you pass parameters to pester test?

Starting from Pester 5.1 you can use New-PesterContainer -Data @{} to pass all required parameters to Invoke-Pester . You can pass now path to both a single test file or a test directory to Invoke-Pester -Path .

How do you mock a function in Pester?

If you wish to mock commands that are called from inside a script module, you can do so by using the -ModuleName parameter to the Mock command. This injects the mock into the specified module. If you do not specify a module name, the mock will be created in the same scope as the test script.

Should I be in Pester?

Should is a keyword that is used to define an assertion inside an It block. Should provides assertion methods to verify assertions e.g. comparing objects. If assertion is not met the test fails and an exception is thrown.

What is pester framework?

Pester is a test framework meant for PowerShell and is a module you can install. It has several features: Assertions. Pester comes with diverse ways of asserting conditions that will determine if your tests should fail or not. Able to run tests.


I'm trying to figure out how to have Pester test for parameters that are missing:

Find-Waldo.Tests.ps1

$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'

Describe 'Mandatory paramters' {
    it  'ComputerName' {
        {
            $Params = @{
                #ComputerName = 'MyPc'
                ScriptName   = 'Test'
            }
            . "$here\$sut" @Params
        } | Should throw
    }
}

Find-Waldo.ps1

Param (
    [Parameter(Mandatory)]
    [String]$ComputerName,
    [String]$ScriptName
)

Function Find-Waldo {
    [CmdletBinding()]
    Param (
        [String]$FilePath
    )

    'Do something'
}

Every time I try to assert the result or simply run the test, it will prompt me for the ComputerName parameter instead of failing the test.

Am I missing something super obvious here? Is there a way to test for the presence of mandatory parameters?