繁体   English   中英

无法模拟 start-process powershell {} 命令?

[英]Not able to mock start-process powershell {} command?

我正在使用一个命令,我将一个 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM