py.test patch on fixture

I’d say patching via decorator is not the optimal approach here. I’d use the context manager: import pytest from unittest.mock import patch @pytest.fixture(autouse=True) def no_delay(): with patch(‘ConstantsModule.ConstantsClass.DELAY_TIME’, 10): yield This way, patching is cleanly reverted on test teardown.

Python: How do I mock datetime.utcnow()?

in your test file: from yourfile import get_report_month_key import mock import unittest from datetime import datetime class TestCase(unittest.TestCase): @mock.patch(‘yourfile.datetime’) def test_dt(self, mock_dt): mock_dt.utcnow = mock.Mock(return_value=datetime(1901, 12, 21)) r = get_report_month_key() self.assertEqual(‘190112’, r)

Mocking extension function in Kotlin

I think MockK can help you. It supports mocking extension functions too. You can use it to mock object-wide extensions: data class Obj(val value: Int) class Ext { fun Obj.extensionFunc() = value + 5 } with(mockk<Ext>()) { every { Obj(5).extensionFunc() } returns 11 assertEquals(11, Obj(5).extensionFunc()) verify { Obj(5).extensionFunc() } } If you extension is a … Read more

react jest mock useNavigate()

I had a similar concern that was fixed with this issue from react router I would suggest you change the mock as is: // pay attention to write it at the top level of your file const mockedUsedNavigate = jest.fn(); jest.mock(‘react-router-dom’, () => ({ …jest.requireActual(‘react-router-dom’) as any, useNavigate: () => mockedUsedNavigate, })); // your describe/it/test … Read more

Testing after_commit with RSpec and mocking

I thought Mihail Davydenkov’s comment deserved to be an answer: You can also use subject.run_callbacks(:commit). Also note that this issue (commit callbacks not getting called in transactional tests) should be fixed in rails 5.0+ so you may wish to make a note to remove any workarounds you may use in the meantime when you upgrade. … Read more

How do you mock patch a python class and get a new Mock object for each instantiation?

Here’s a quick’n’dirty example to get you going: import mock import unittest class ClassToPatch(): def __init__(self, *args): pass def some_func(self): return id(self) class UUT(): def __init__(self, *args): resource_1 = ClassToPatch() resource_2 = ClassToPatch() self.test_property = (resource_1.some_func(), resource_2.some_func()) class TestCase1(unittest.TestCase): @mock.patch(‘__main__.ClassToPatch’, autospec = True) def test_1(self, mock1): ctpMocks = [mock.Mock(), mock.Mock()] ctpMocks[0].some_func.return_value = “funky” ctpMocks[1].some_func.return_value = … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)