首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >删除Pester模拟函数

删除Pester模拟函数
EN

Stack Overflow用户
提问于 2019-11-27 09:18:24
回答 1查看 653关注 0票数 3

如何才能对以前模拟的函数进行unmock?有时,我发现自己处于一种情况下,我想测试以前的mocked函数。

一个简化的例子:

代码语言:javascript
复制
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
    }
}
EN

回答 1

Stack Overflow用户

发布于 2019-11-27 10:03:29

看来,Pester为每个模拟的函数创建了一个alias。解决方案是从作用域中删除alias。这样,真正的CmdLet就会被调用。

根据您的PowerShell版本,有两种方法可以做到这一点

代码语言:javascript
复制
Remove-Item Alias:\Out-File
Remove-Alias Out-File

解决方案:

代码语言:javascript
复制
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
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59066763

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档