In jest, how do I use “toHaveBeenCalledWith” and only match part of an object in an array argument?

You can use a combination of arrayContaining and objectContaining to make this work. Reference: https://jestjs.io/docs/expect#expectarraycontainingarray https://jestjs.io/docs/expect#expectobjectcontainingobject Here is some sample code for you: function something(a, b, somefn) { somefn([{ x: a, y: b, id: ‘some-guid’ }]); } test(‘Testing something’, () => { const mockSomeFn = jest.fn(); something(2, 3, mockSomeFn); expect(mockSomeFn).toHaveBeenCalledWith( expect.arrayContaining([ expect.objectContaining({ x: 2, y: … Read more

How do you change the behaviour of a mocked import in Jest?

After you’ve mocked the module and replaced the methodToMock with a spy, you need to import it. Then, at each test, you can change the behaviour of methodToMock by calling the mockImplementation spy method. jest.mock(‘the-package-to-mock’, () => ({ methodToMock: jest.fn() })) import { methodToMock } from ‘the-package-to-mock’ it(‘Test A’, () => { methodToMock.mockImplementation(() => ‘Value … Read more

Mocking default=timezone.now for unit tests

Here’s a method you can use that doesn’t require altering your non-test code. Just patch the default attributes of the fields you want to affect. For example– field = User._meta.get_field(‘timestamp’) mock_now = lambda: datetime(2010, 1, 1) with patch.object(field, ‘default’, new=mock_now): # Your code here You can write helper functions to make this less verbose. For … Read more

python check if a method is called without mocking it away

You can set the Mock.side_effect to be the original method. from unittest.mock import MagicMock class A(): def tmp(self): print(“hi”) def b(a): a.tmp() a = A() a.tmp = MagicMock(side_effect=a.tmp) b(a) a.tmp.assert_called() When side_effect is a function (or a bound method in this case, which is a kind of function), calling the Mock will also call the … Read more

Mock exception raised in function using Pytest

You can mock error raising via side_effect parameter: Alternatively side_effect can be an exception class or instance. In this case the exception will be raised when the mock is called. In your case, this can be used like this (assuming call_api is defined in module foo): import pytest from unittest.mock import patch def test_api(): with … Read more

Advice on Mocking System Calls

In this case you don’t need to mock getaddrinfo, rather, you need to test without relying on its functionality. Both Patrick and Noah have good points but you have at least two other options: Option 1: Subclass to Test Since you already have your object in a class, you can subclass to test. For example, … Read more

How to capture variable parameters with Mockito?

Mockito 1.10.5 has introduced this feature. For the code sample in the question, here is one way to capture the varargs: ArgumentCaptor<String> varArgs = ArgumentCaptor.forClass(String.class); A mock = Mockito.mock(A.class); mock.setNames(“Jeff”, “Mike”, “John”); Mockito.verify(mock).setNames(varArgs.capture()); //Results may be validated thus: List<String> expected = Arrays.asList(“Jeff”, “Mike”, “John”); assertEquals(expected, varArgs.getAllValues()); Please see the ArgumentCaptor javadoc for details.

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