尝试在 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
,因为我不想实际删除用户只是测试逻辑并且还能够$result
使用 -Foreach 迭代变量。
$result
变量包含withpscustomobjects
和id
属性displayname
。我正在用 2 个这样的对象来测试函数。
$cloudusers = @([pscustomobject]@{id=1;displayname="john"},
[pscustomobject]@{id=2;displayname="doe"})
更新:我想我需要改变测试数据的方式,所以关闭它