如何才能对以前模拟的函数进行unmock?有时,我发现自己处于一种情况下,我想测试以前的mocked函数。
一个简化的例子:
Describe 'Pester mocking' {
$testFile = Join-Path $env:TEMP 'test.txt'
It 'should be green' {
Mock Out-File
'Text' | Out-File -FilePath $testFile
Assert-MockCalled Out-File -Times 1 -Exactly
}
It 'should be green' {
# Unmock Out-File
'Text' | Out-File -FilePath $testFile
$testFile | Should -Exist
}
}发布于 2019-11-27 10:03:29
看来,Pester为每个模拟的函数创建了一个alias。解决方案是从作用域中删除alias。这样,真正的CmdLet就会被调用。
根据您的PowerShell版本,有两种方法可以做到这一点
Remove-Item Alias:\Out-File
Remove-Alias Out-File解决方案:
Describe 'Pester mocking' {
$testFile = Join-Path $env:TEMP 'test.txt'
It 'should be green' {
Mock Out-File
'Text' | Out-File -FilePath $testFile
Assert-MockCalled Out-File -Times 1 -Exactly
}
It 'should be green' {
Remove-Item Alias:\Out-File
'Text' | Out-File -FilePath $testFile
$testFile | Should -Exist
}
}https://stackoverflow.com/questions/59066763
复制相似问题