I can't figure out how I can mock my function in python, I've tried to look for some code, but no of it seems to work, consider this layout:
| project.py
| tests.py
project.py:
def foobar():
    return
tests.py:
import unittest
from unittest.mock import patch
import os
from project import foobar
def os_urandom_mock():
    return 'mocked'
def foobar_mock():
    return 'mocked'
class TestProject(unittest.TestCase):
    # mocked os.urandom worked well
    @patch('os.urandom', side_effect=os_urandom_mock)
    def test_os_urandom_mocked(self, os_urandom_mocked):
        self.assertEqual(os.urandom(), 'mocked')
    # but this doesn't
    @patch('project.foobar', side_effect=foobar_mock)
    def test_foobar_mocked(self, foobar_mocked):
        self.assertEqual(foobar(), 'mocked')
    # and this also doesn't work
    @patch('project.foobar')
    def test_foobar_mocked_another(self, foobar_mocked):
        foobar_mocked.return_value = 'mocked'
        self.assertEqual(foobar(), 'mocked')
    # and this also doesn't work
    def test_foobar_mocked_last_try(self):
        with patch('project.foobar') as foobar_mocked:
            foobar_mocked.return_value = 'mocked'
            self.assertEqual(foobar(), 'mocked')
unittest.main()
so, python3 tests.py:
======================================================================
FAIL: test_foobar_mocked (__main__.TestProject)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python36\lib\unittest\mock.py", line 1179, in patched
    return func(*args, **keywargs)
  File "tests.py", line 24, in test_foobar_mocked
    self.assertEqual(foobar(), 'mocked')
AssertionError: None != 'mocked'
======================================================================
FAIL: test_foobar_mocked_another (__main__.TestProject)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python36\lib\unittest\mock.py", line 1179, in patched
    return func(*args, **keywargs)
  File "tests.py", line 30, in test_foobar_mocked_another
    self.assertEqual(foobar(), 'mocked')
AssertionError: None != 'mocked'
======================================================================
FAIL: test_foobar_mocked_last_try (__main__.TestProject)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "tests.py", line 35, in test_foobar_mocked_last_try
    self.assertEqual(foobar(), 'mocked')
AssertionError: None != 'mocked'
----------------------------------------------------------------------
Ran 4 tests in 0.002s
FAILED (failures=3)
As you can see test_os_urandom_mocked is ok, but all other tests where I've tried to mock my foobar function have failed, don't know why, can anyone please explain if it's possible to do?
 
    