Top "Mocking" questions

Mocking and faking are ways to isolate code or components to ensure that unit tests run against the testable unit of code only without actually utilizing other components or dependencies of an application.

How do Mockito matchers work?

Mockito argument matchers (such as any, argThat, eq, same, and ArgumentCaptor.capture()) behave very differently from Hamcrest matchers. Mockito matchers …

java mocking mockito
How do I mock an open used in a with statement (using the Mock framework in Python)?

How do I test the following code with unittest.mock: def testme(filepath): with open(filepath) as f: return f.…

python mocking with-statement
Mocking Extension Methods with Moq

I have a preexisting Interface... public interface ISomeInterface { void SomeMethod(); } and I've extended this intreface using a mixin... public static …

c# unit-testing mocking extension-methods moq
How to mock the JavaScript window object using Jest?

I need to test a function which opens a new tab in the browser openStatementsReport(contactIds) { window.open(`a_url_${…

javascript mocking jestjs
How to spyOn a value property (rather than a method) with Jasmine

Jasmine's spyOn is good to change a method's behavior, but is there any way to change a value property (rather …

javascript unit-testing mocking jasmine
Python mock multiple return values

I am using pythons mock.patch and would like to change the return value for each call. Here is the …

python unit-testing mocking python-mock
Using Mockito, how do I verify a method was a called with a certain argument?

I'm using Mockito 1.9.0. How would i verify that a method got called exactly once, and that one of the fields …

java mocking mockito
Asserting successive calls to a mock method

Mock has a helpful assert_called_with() method. However, as far as I understand this only checks the last call …

python mocking
Mocking a function to raise an Exception to test an except block

I have a function (foo) which calls another function (bar). If invoking bar() raises an HttpError, I want to handle …

python unit-testing python-2.7 mocking python-mock
Mocking a class: Mock() or patch()?

I am using mock with Python and was wondering which of those two approaches is better (read: more pythonic). Method …

python unit-testing mocking