powershell - 删除 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
}
}
解决方案
弄清楚了,似乎为每个模拟函数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
}
}
推荐阅读
- reactjs - 将为 jsx 编写的“enquireScreen”js 转换为打字稿?
- php - Laravel 气闸与 Laravel 护照
- html - 我的网站在 chrome 中表现怪异,但在 Firefox/Internet Explorer 上运行良好
- algorithm - 这个函数的时间复杂度是多少?
- html - 如何在不使用“text-align:center”的情况下将文本放在 div 的中心?
- java - JTable 显示值错误/不显示整个值
- php - PHPUnit 模拟函数所以它不被调用
- python - 在 HDF 中附加多索引数据帧
- python - 如何在python中选择和排序列的元素?
- java - Java Processbuilder 输出流