This is about Python Mocker features. In order to pass in a mock as a parameter to the SUT (subject under test) you might think of creating the mock using either, “mock()” method or “proxy(SomeClass)” method. They both yield mocks but they work differently.
- mock() should be used for stubs and to mock out functions which are not contained in any class but in mofules.
- proxy(SomeClass) should be used to mock out classes. Mocker checks that the expectation set on the mock matches an existent method in the class we’re proxying. This adds value to the test as it enforces API matching, which is very valuable in Python.
def test_whatever(): mockMyClass = self.mocker.proxy(MyClass) mockMyClass.someMethod() self.mocker.count(1) self.mocker.replay() mySutInstance = MySut(mockMyClass) result = mySutInstance.methodUnderTest() self.assertTrue(result) self.mocker.verify()
In the code above, if “someMethod” wasn’t existent in MyClass, that would raise an exception. Using mock(), the test would run with same outcome but no exception, so it misses part of the point.