模拟内部函数在模块中引发异常

时间:2018-06-21 05:03:51

标签: powershell pester

使用Pester 4.3.1为PowerShell模块文件编写单元测试。

我可以通过模块名称开关在同一模块内调用导出的功能

Mock -ModuleName myModule exportedCommand

从模拟命令返回任何值时,模拟可以正常工作

Mock -ModuleName myModule exportedCommand -Mockwith {return "somevalue"}

但是我无法通过从模拟调用中抛出异常来执行模拟

Mock -ModuleName myModule exportedCommand -Mockwith {throw "some exception"}

下面是带有模拟抛出异常的示例上下文

Context 'sample context'{
    Mock -ModuleName myModule exportedCommand -Mockwith {throw "some exception"}
    (outer-function)
    It 'Should throw exception and fail'{
        Assert-MockCalled -ModuleName myModule exportedCommand -Exactly -Times 1
        Assert-VerifiableMock
    }
}

上下文将在控制台上引发异常

[-] Error occurred in Context block                          
                  RuntimeException: ScriptHalted
        at ExecuteBlock, D:\CodeBase\Collection_EICI\ServiceTools\EI\PlatformUpgradeTool\branches\Dev\packages\Pester.4.3.1\tools\Functions\Mock.ps1: line 1123
        at Invoke-Mock, D:\CodeBase\Collection_EICI\ServiceTools\EI\PlatformUpgradeTool\branches\Dev\packages\Pester.4.3.1\tools\Functions\Mock.ps1: line 966

0 个答案:

没有答案