1

trying to test a function in pester 5.3.1 (latest) and ps7.2.1

    function Remove-GraphUser
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [object]$cloudUsers
    )
    process
    {
        foreach ($User in $cloudUsers)
        {
            Remove-MgUser -UserId $User.id
            $user
        }
    }
}

What is the best way to mock the remove-mguser function and test the resulting array of user objects output by the function remove-graphuser

tried this: but it seems to skip mock defined in beforeall.

    BeforeDiscovery {
    $cloud = Import-Csv $PSScriptRoot\files\cloud.csv
    $result = Remove-GraphUser -cloud $cloud -Verbose
}

Describe 'Remove-GraphUser' -tags 'two' {
    BeforeAll {
        Mock Remove-MgUser {}
    }

    It 'should be called twice' {
        Assert-MockCalled -CommandName Remove-MgUser -Times 2 -Scope describe
    }
    It '<_.id> should be removed from the cloud group' -ForEach $result {
        $_.id | Should -Not -Be $null
    }
}

this does not work either(the foreach it block is skipped but assert-mock runs)

    BeforeAll {
    Mock Remove-MgUser {}
    $cloud = Import-Csv $PSScriptRoot\files\umbrella1_cloud.csv
    $result = remove-graphuser -cloud $cloud -Verbose
}

Want to be able to mock remove-mggraphuser because i dont want to actually remove users just test the logic and also be able to iterate over the $result variable using -Foreach.

the $result variable contains pscustomobjects with id and displayname properties. I am tsting the function with 2 such objects.

 $cloudusers = @([pscustomobject]@{id=1;displayname="john"}, 
   [pscustomobject]@{id=2;displayname="doe"})

Update: I think I need to change the way i am testing the data so closing this

Kiran Reddy
  • 2,836
  • 2
  • 16
  • 20
  • yeah I am just going through trial and error at this point even if things don't make sense. Anyway I am just testing a function which I have dot sourced in another `beforeall` block. There isnt a module at this point. – Kiran Reddy Feb 01 '22 at 03:38
  • thank you for testing...i should have also added the output of the function...so the function outputs pscustomobjects(id and displayname properties). I am testing with 2 such user objects so the `testcases` should iterate over the 2 objects. I am able to get the mock to work in beforeall but the `testcases` wont run because the `$result` var is null which shouldnt be. – Kiran Reddy Feb 01 '22 at 14:13
  • yeah its kind of related to how pester5 phases work and what executes in which phase. I will post the answer. – Kiran Reddy Feb 02 '22 at 07:16

1 Answers1

1

-ForEach is processed during the Discovery-phase, while BeforeAll is processed later during the Run-phase so the solution is to use a foreach loop inside the it block.

BeforeDiscovery {
$cloud = Import-Csv $PSScriptRoot\files\cloud.csv
$result = Remove-GraphUser -cloud $cloud -Verbose

}

Describe 'Remove-GraphUser' -tags 'two' {
BeforeAll {
    Mock Remove-MgUser {}
}

It 'should be called twice' {
    Assert-MockCalled -CommandName Remove-MgUser -Times 2 -Scope describe
}
It user should be removed from the cloud group' -ForEach $result {
   foreach ($index in $result)
    {
        $index.id | Should -Not -Be $null
        $index.ad_id | Should -Be $null
    }

}
}
Kiran Reddy
  • 2,836
  • 2
  • 16
  • 20