Pester icon indicating copy to clipboard operation
Pester copied to clipboard

Should -Invoke test from 5.1.1 fails on 5.2.2

Open efie45 opened this issue 4 years ago • 10 comments
trafficstars

General summary of the issue

Using Pester 5.1.1 I had something like this:

It "calls Write-Progress" {
            $tracker = [myCustomClass]::new(@(1))
            Should -Invoke Write-Progress -Exactly 1
}

The constructor from [myCustomClass] would call a function that called another function which called Write-Progress. In 5.2.2 the above test fails where it would pass previously. What is going on here and how do I fix it?

Describe your environment

Pester version : 5.2.2 /Users/____/.local/share/powershell/Modules/Pester/5.2.2/Pester.psm1 PowerShell version : 7.1.3 OS version : Unix 11.0.0

EDIT

I guess I should edit to mention I found a solution but I may be looking for another one.

I was able to get it working again by specifying the module that Write-Progress should be called from and then again in Should-Invoke with -ModuleName.

So the question remains:

  • What do you specify for Mock and Should -Invoke when you want to see how many times something was called from any module?

efie45 avatar Jun 14 '21 20:06 efie45

What do you specify for Mock and Should -Invoke when you want to see how many times something was called from any module?

There is no such option, but I was proposing to do it by *, if there is a need to do that. I don't think we will be able to get it to 5.3, there is already a lot planned for that milestone. But if you or @fflaten want to implement it go ahead, I will gladly merge it early.

nohwnd avatar Jun 15 '21 08:06 nohwnd

There is no such option, but I was proposing to do it by *, if there is a need to do that.

Seems logical as an option in Should -Invoke to quickly assert globally. Wildcard search in the mock call history should be doable.

Or did you also consider it in Mock as "publish in all modules"/fallback behavior (have I seen this in another issue or PR comment?)?

fflaten avatar Jun 15 '21 10:06 fflaten

I did not. Seems like overkill that would be rarely used, and would be very costly to use as we would have to define hooks in all modules that are currently loaded.

nohwnd avatar Jun 15 '21 10:06 nohwnd

Exactly, just checking because I believe we mentioned something about looking into this as "fallback" when making -ModuleName more strict in 5.2.

I might take a look at the Should Invoke option unless @efie45 wants to give it a shot. 🙂

fflaten avatar Jun 15 '21 10:06 fflaten

Wildcard search in the mock call history should be doable.

Yeah that should work just fine, you just need to avoid filtering down to the mocks that are targetting the given module. I don't remember where exactly we do that, but if you stop in a mock filter when calling Should -Invoke you will be able to find it easily in the call stack I think.

nohwnd avatar Jun 15 '21 10:06 nohwnd

I'd love to give it a shot. I've never contributed before though so it may take me a little longer

efie45 avatar Jun 15 '21 12:06 efie45

@efie45 Assigning it to you. If you have any questions ask. We are here to help you.

Here is basic contribution guide: https://github.com/pester/Pester/blob/main/CONTRIBUTING.md

nohwnd avatar Jun 15 '21 12:06 nohwnd

Great! I'm stoked to give it a try. Is there a discord or any other channel for communication for questions about contributions or should that just be done here?

efie45 avatar Jun 15 '21 12:06 efie45

We don't have a special channel just for contributions. You can ask here, or ping me on twitter via DM if you prefer. Or create a discussion in https://github.com/pester/Pester/discussions or ask in the testing channel on slack https://app.slack.com/client/T03MS9QKY/C03QKTUCS

nohwnd avatar Jun 15 '21 12:06 nohwnd

Friendly reminder @efie45. Do you still want to attempt this? 🙂

fflaten avatar Jul 06 '22 13:07 fflaten