当使用invoke-pester

时间:2019-03-15 16:25:34

标签: powershell pester

以下脚本在带点运行时有效,但在用invoke-pester(运行Pester版本4.6.0)调用时无法运行。我是在做错什么还是invoke-pester有问题?

using module "./Test.psm1"
Describe "Unit Test" -Tag 'Unit' {
  Context 'test' {
    It 'return' {
        mock Write-Verbose { write-host $Message }
        $T = [Test]::new()
        $T.createoutput()
        assert-MockCalled Write-Verbose
    }
  }
}

Test.psm1:

class Test{
    [void]createoutput(){
        Write-Verbose 'hello'
    }
}

1 个答案:

答案 0 :(得分:1)

问题出在您的模拟设置上。模拟模块时,there is some additional work required

实际情况:

using module ./Test.psm1

Describe 'Unit Test' -Tag 'Unit' {
    Context 'test' {
        It 'mocks correctly' {
            Mock Write-Verbose {} -ModuleName Test

            $T = [Test]::new()
            $T.createoutput()

            Assert-MockCalled Write-Verbose -ModuleName Test
        }
    }
}

也很重要,read this note about class importing