New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Testing of ForEach-Object -Parallel #2356
Comments
Unfortunately that's not possible. Using |
Sorry to hear that it's not possible. Any recommendation or best practises on how to implement multithreading in powershell in order to make it testable with Pester? |
It's mostly mocking objects and commands that won't work. You can test the results of any code using multithreading, so maybe focus on integration tests against test-data/environments? |
Running into this same limitation of Pester. Some easy repro code: Describe 'it should be' {
BeforeAll {
$realCmdLet = @{
InvokeCommand = Get-Command Invoke-Command
}
Mock Invoke-Command {
& $realCmdLet.InvokeCommand -Scriptblock {
1
} -ComputerName $env:COMPUTERNAME
}
$testData = @(
@{ComputerName = 'PC1' }
@{ComputerName = 'PC2' }
)
}
It 'green' {
$testData | ForEach-Object {
Invoke-Command -ComputerName $_.ComputerName -ScriptBlock { 1 }
}
Should -Invoke Invoke-Command -Times 1 -Exactly -ParameterFilter {
$ComputerName -eq 'PC1'
}
Should -Invoke Invoke-Command -Times 1 -Exactly -ParameterFilter {
$ComputerName -eq 'PC2'
}
}
It 'green' {
$testData | ForEach-Object -Parallel {
Invoke-Command -ComputerName $_.ComputerName -ScriptBlock { 1 }
}
Should -Invoke Invoke-Command -Times 1 -Exactly -ParameterFilter {
$ComputerName -eq 'PC1'
}
Should -Invoke Invoke-Command -Times 1 -Exactly -ParameterFilter {
$ComputerName -eq 'PC2'
}
}
} The last test fails because of @nohwnd @fflaten is there really no way around this? It would maybe be an idea if Pester could remove the Related StackOverflow question. |
Checklist
Summary of the feature request
I would like to be able to mock a function inside of a "ForEach-Object -Parallel". In the above rather contrieved example I would like to be able to mock the Start-Sleep function
Thx
How should it work?
No response
The text was updated successfully, but these errors were encountered: