无法模拟启动进程电源外壳 {} 命令?



我正在使用一个命令,我将一个ps1(dummy.ps1(文件调用到另一个ps1文件, 我正在使用的命令是:

start-process powershell{.dummy.ps1}

我需要编写纠缠单元测试来覆盖这一行,我必须模拟它,但它没有发生,所以请帮助我解决这个问题。

我像这样写模拟:

Mock start-process {} 

在我的测试文件中。

我认为您尝试的内容没有任何问题,尽管在您尝试的完整上下文中可能存在问题。

我能够模拟启动过程,如下所示:

Describe 'tests involving start-process' {
Context 'no mock' {
It 'needs to be mocked' {
# demonstrate that it's not mocked 
# ... you'll have to dismiss the console window by hand
start-process cmd "/c pause" | should -Be $null
}
}
Context 'mocked' {
Mock start-process {"fake execution"}
It 'can be mocked' {
start-process cmd "/c pause" | should -Be "fake execution"
}
}
}

这将产生以下输出:

> Invoke-Pester ".test.tests.ps1"
____            __
/ __ ___  _____/ /____  _____
/ /_/ / _ / ___/ __/ _ / ___/
/ ____/  __(__  ) /_/  __/ /
/_/    ___/____/__/___/_/
Pester v4.9.0
Executing all tests in '.test.tests.ps1'
Executing script .test.tests.ps1
Describing tests involving start-process
Context no mock
[+] needs to be mocked 27ms
Context mocked
[+] can be mocked 14ms
Tests completed in 348ms
Tests Passed: 2, Failed: 0, Skipped: 0, Pending: 0, Inconclusive: 0 

也许您需要安装最新的 Pester? 我用install-module pester -force -skippublishercheck更新到 4.9

最新更新