Python Forum
Mock obj - How to call the side_effect every time during the run?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mock obj - How to call the side_effect every time during the run?
#1
I need to mock an object with a bunch of methods. For all except one Mock() is perfect for me. But for one of the methods, I actually need to call a function whenever the method is called during the run.

Basically, I am doing some basic threading, so for my code to work, I can't give it the return values manually before the run. The function has to be called during the run.

m = Mock()
m.meth.side_effect = foo()
When I do this, as expected, it calls foo() at the start, and uses that value permanently

So, I would like it such that whenever during the run - m.meth is called - foo() is called (and value returned)
Reply


Messages In This Thread
Mock obj - How to call the side_effect every time during the run? - by pythonisbae - Mar-05-2022, 03:07 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  unittest.mock for an api key silver 3 1,407 Aug-29-2022, 03:52 PM
Last Post: ndc85430
  function call at defined system time? Holon 5 3,258 Oct-06-2020, 03:58 PM
Last Post: snippsat
  StopIteration exception when mock PostgreSQL connection in several tests igor87z 1 2,944 Jun-10-2020, 06:16 PM
Last Post: ibreeden
  How to mock an object that is created during function call? Schlangenversteher 0 1,987 Jan-31-2020, 01:36 PM
Last Post: Schlangenversteher
  Mock call to subprocess.Popen failing with StopIteration tharpa 7 5,969 Nov-08-2019, 05:00 PM
Last Post: Gribouillis
  unittest mock and patch piscvau 1 2,136 Nov-08-2019, 03:23 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020