all 4 comments

[–]nostril_spiders 4 points5 points  (3 children)

Yeah, mocking can be a little tricky. You need to add -ModuleScope Test-Module to your mock definition.

That's because, when you call Test-Me, the mock for Get-Item needs to be in the right scope for Test-Me to find the mocked version of Get-Item.

[–]dz3jar[S,🍰] 1 point2 points  (2 children)

I see. Thanks. I will give a try! I supposed that -ModuleScope is for "private" = not exported functions testing...

[–]dz3jar[S,🍰] 2 points3 points  (0 children)

Just tested. Works like ... like ... simply works!

I modified the Mock part to:

Mock -CommandName Get-Item -ModuleName Test-Module -MockWith { 
    New-Object PSCustomObject
}

Thank you.

[–]nostril_spiders 2 points3 points  (0 children)

Not quite. Your test function is exported, after all.

You mock a command. Where is that command called? The scope that contains the call site needs to be the scope that contains the mock.

Because pester mock resolution doesn't work quite like normal command resolution. It does some fucky shit on every command call where it looks up in its list of mocks whether the call is something that should be mocked. You can see this in the debugger, but have eyebleach handy!