首页 > 解决方案 > 删除 Pester 模拟函数

问题描述

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

标签: powershellmockingpester

解决方案


弄清楚了,似乎为每个模拟函数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
    }
}

推荐阅读