# Weird behaviour - mock hangs when being called twice

**URL:** <https://forums.powershell.org/t/weird-behaviour-mock-hangs-when-being-called-twice/24890>\
**Category:** Pester\
**Created:** [October 18, 2024, 8:36am UTC](https://forums.powershell.org/t/weird-behaviour-mock-hangs-when-being-called-twice/24890 "2024-10-18T08:36:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dandraka](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dandraka/32/5690_2.png) [@dandraka](https://forums.powershell.org/u/dandraka)\
**Post date:** [October 18, 2024, 8:36am UTC](https://forums.powershell.org/t/weird-behaviour-mock-hangs-when-being-called-twice/24890/1 "2024-10-18T08:36:45Z")

</div>

Hi all

I’m assigned to write a bunch of tests on an existing codebase and a number of my tests hang.

The code, simplified to show the issue, is as follows:

File MySharepointModule.ps1 :

```auto
function Update-SharepointList {
  # do some stuff
  Set-PnPListItem -someparams
}

```

File MyScript.ps1 :

```auto
Import-Module -Name '.\Modules\MySharepointModule.ps1'
# do lots of stuff
Set-PnPListItem -someparams

# do lots of stuff
Update-SharepointList

```

In order to avoid setting up a Sharepoint site, I’m mocking Set-PnPListItem.

```auto
Mock Set-PnPListItem { 
    # store args into a variable
}

```

It works for the first call. But on the second call, where it’s being called from the module, Set-PnPListItem hangs. It doesn’t even throw an error -just hangs. And if I need to run the tests again, I have to kill the terminal or restart VSCode.

I’ve tried different variations, even leaving the mock empty. Nothing works, it always hangs. The only way I can get it to work is if I mock Update-SharepointList itself, but that means leaving this code untested.

Is this a known issue, behaviour or bug? Are there any workarounds?

Many thanks in advance

Jim

---

<div class="post-metadata">

**Author:** ![dandraka](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dandraka/32/5690_2.png) [@dandraka](https://forums.powershell.org/u/dandraka)\
**Post date:** [October 18, 2024, 12:24pm UTC](https://forums.powershell.org/t/weird-behaviour-mock-hangs-when-being-called-twice/24890/2 "2024-10-18T12:24:35Z")

</div>

_I asked the same question in Github and got an answer, so I’m adding it here:_

> [@](#):
>
> Hi. This is by design. The mock isn’t called when invoked through `Update-SharepointList` as it invokes from another state (the module).
> 
> You need two mocks for you sample.
> 
> - The one you have to catch the call done directly in the test.
> - Another one specifically for calls made from your module functions
> 
> Try adding:
> 
> ```auto
> Mock Set-PnPListItem -ModuleName MySharepointModule { 
> # store args into a variable 
> }
> 
> ```
> 
> See [Unit Testing within Modules | Pester](https://pester.dev/docs/usage/modules)

Link to answer:

> **[Weird behaviour - mock hangs when being called twice · pester/Pester ·...](https://github.com/pester/Pester/discussions/2569#discussioncomment-10982685)**
>
> Hi all (I've posted the same question in Powershell forums but on a second thought, here is probably the best place to ask) I’m assigned to write a bunch of tests on an existing codebase and a numb...

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [November 17, 2024, 12:24pm UTC](https://forums.powershell.org/t/weird-behaviour-mock-hangs-when-being-called-twice/24890/3 "2024-11-17T12:24:46Z")

</div>

Closed on November 17, 2024, 12:24pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
