orange-lily
orange-lily

Reputation: 221

Mock write-verbose not working when with invoke-pester

The below script works when ran with a dot but fail to run when called with invoke-pester (running Pester version 4.6.0). Am I doing something wrong or is there an issue with 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'
    }
}

Upvotes: 2

Views: 1014

Answers (1)

Maximilian Burszley
Maximilian Burszley

Reputation: 19684

The problem is with your mock setup. When mocking for modules, there is some additional work required.

In action:

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
        }
    }
}

Also important, read this note about class importing.

Upvotes: 2

Related Questions