Pester 5 mocking 和 beforediscovery

Pester 5 mocking and beforediscovery

尝试测试 pester 5.3.1(最新)和 ps7.2.1

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

模拟 remove-mguser 函数并测试函数输出的用户对象结果数组的最佳方法是什么 remove-graphuser

试过这个:但它似乎跳过了之前定义的模拟。

    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
    }
}

这也不起作用(foreach it 块被跳过但 assert-mock 运行)

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

希望能够模拟 remove-mggraphuser 因为我不想实际删除用户只是测试逻辑并且还能够使用 -Foreach 迭代 $result 变量。

$result 变量包含具有 iddisplayname 属性的 pscustomobjects。我正在用 2 个这样的对象测试函数。

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

更新:我想我需要改变我测试数据的方式所以关闭这个

-ForEach 在 Discovery-phase 期间处理,而 BeforeAll 在 Run-phase 期间处理,因此解决方案是在 it 块内使用 foreach 循环。

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
    }

}
}