小编Ben*_*itM的帖子

Powershell 5类的Pester模拟方法

我在尝试模拟powershell 5类方法时遇到问题,在执行测试时,我收到错误"CommandNotFoundException:找不到Command FunctionToMock".我试图通过模拟"FunctionToMock"来单元测试"OutputToOverwrite"方法.我想我必须首先嘲笑ChocoClass本身,但我不知道该怎么做.谢谢.

Class ChocoClass
{
    [string] OutputToOverwrite()
    {
        return $this.FunctionToMock()
    }

    [string] FunctionToMock()
    {
        return "This text will be replaced"
    }
}


Describe "Testing mocking"{
    it "Mock test"{
        Mock FunctionToMock -MockWith {return "mystring"}
        $package = New-Object ChocoClass
        $expected = $package.OutputToOverwrite()
        $expected | should BeExactly "mystring"
    }
}
Run Code Online (Sandbox Code Playgroud)

powershell class pester

5
推荐指数
1
解决办法
1150
查看次数

标签 统计

class ×1

pester ×1

powershell ×1