How to provide multiple mocks to pester?

Welcome Forums Pester How to provide multiple mocks to pester?

This topic contains 1 reply, has 2 voices, and was last updated by

1 year, 9 months ago.

  • Author
  • #70636

    Points: 0
    Rank: Member

    I'm a newbie to Pester so please forgive me if this has been answered already. I want to run multiple unit tests on a PowerShell function, providing a range of mocks to exercise the function. Below is the function and test code I've written. I'd like to mock Get-WASJob multiple times, providinga seperate mock to each unit test.
    How do I do this?

    Function GetWASJob {
        param ( [string]$project, [string]$job, [string]$os, [string]$runtype )
        $runjob = Get-WASJob -ProjectName $project -JobName $job -ErrorAction Stop
        #Check if the WAS job is valid
        if ($runjob -and $runjob.JobName) {
            WriteToLog "GetWASJob: Returned a valid WAS job" $logfile
        else {
            WriteToLog "GetWASJob: Valid WAS job not returned...Quit" $logfile
        return $runjob

    Pester code to test the above function:

    Describe "GetWASJob" {
        Context "Just Testing..." {
            Mock Get-WASJob {
                $obj = New-MockObject -Type 'Microsoft.Assessments.WAS.PowerShell.WASJob'
                $obj | Add-Member -Type NoteProperty -Name 'JobName' -Value 'PesterJob1' -Force
                $obj | Add-Member -Type NoteProperty -Name 'Tags' -Value 'PesterTag1' -Force
                return $obj
        	$result = GetWASJob -project "Project1" -job "Job1" -os "Windows10" -runtype "Production"
    	    It "Verified that all mocks were called" {
    	    It "Checks that a valid job object was returned" {
                $result.JobName | Should Be "PesterJob1"
  • #70858

    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    If I'm understanding what you want, you'd use multiple Context blocks for each scenario.

The topic ‘How to provide multiple mocks to pester?’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort