How to mock a function defined in a separate Python module using mock's @patch
Date : March 29 2020, 07:55 AM
I wish did fix the issue. mypackage.myothermodule has already been imported, so the name some_function is already bound within that module. You need to mock the usage of that name within the module it is being called from: @patch('mypackage.myothermodule.some_function')
import mypackage.myothermodule
reload(mypackage.myothermodule)
|
Python mock patch a coroutine function within another function using pytest?
Date : March 29 2020, 07:55 AM
Does that help In your second example, in mock_f1 fixture you create a CoroutineMock object and return it. But you don't overwrite module_name.app.f1 function: Mock-like objects don't patch anything automatically. Here is an illustrating addition to your example: @pytest.mark.asyncio
async def test_f2_2(mock_f1):
print('fixture value:', mock_f1)
print('actual module_name.app.f1 function:', module_name.app.f1)
assert 'some value' == await f2('test')
fixture value: <CoroutineMock spec='function' id='139756096130688'>
actual module_name.app.f1 function: <function f1 at 0x7f1b7e1139d8>
@pytest.fixture
def mock_f1(monkeypatch):
fake_f1 = asynctest.CoroutineMock(module_name.app.f1, side_effect=sf_f1)
monkeypatch.setattr(module_name.app, 'f1', fake_f1)
return fake_f1
|
Im trying to mock a function from a service but Jest keeps calling the actual function instead of the mock function
Date : March 29 2020, 07:55 AM
hop of those help? The problem is that Jest keeps calling the actual services function instead of the mocked service function. const mockService = require('../NotificationService').default; // mockService is your mock...
jest.mock('../NotificationService'); // ...because this runs first
describe('NotificationService.js', () => {
it('returns the bell property', async () => {
...
});
});
|
Python Mock: Mocking a function inside the function I'm testing
Date : March 29 2020, 07:55 AM
this one helps. What you need is the patch.object context manager, which does exactly what you want: it takes a class and method, and allows you to define a custom return value. from unittest.mock import patch
from <your-time_utils-module> import apply_delta_to_date, MySettings
class TestAppyDelta():
def test_apply_delta(self):
with patch.object(MySettings, 'get_delta', return_value=10) as mock_method:
result = apply_delta_to_date(today)
# mock_method has many other useful assertions
mock_method.assert_called_once()
|
How can I mock an object's function call in another function that doesn't necessarily exist because of moto's mock
Date : March 29 2020, 07:55 AM
hope this fix your issue boto3.client returns an instance of a dynamically-created class based on the service_name argument (see source), so you cannot use the patch method, which requires that the target object be importable. Instead, you can patch botocore.client.ClientCreator._create_methods, the method that dynamically creates methods for the class that boto3.client returns, with a wrapper function that makes the describe_continuous_backups attribute a Mock object with the given return_value: import boto3
import botocore
from unittest.mock import patch, Mock
def override(*args, **kwargs):
def wrapper(self, service_model):
op_dict = original_create_methods(self, service_model)
if 'describe_continuous_backups' in op_dict:
op_dict['describe_continuous_backups'] = Mock(*args, **kwargs)
return op_dict
return wrapper
original_create_methods = botocore.client.ClientCreator._create_methods
@patch('botocore.client.ClientCreator._create_methods', override(return_value={'foo': 'bar'}))
def check_stuff():
session = boto3.Session()
client = session.client('dynamodb', 'eu-west-1')
some_stuff = client.describe_continuous_backups(TableName='')
return some_stuff
print(check_stuff())
{'foo': 'bar'}
|