You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We are running regularly into issues where a mock outlives the duration of a test because it gets retained somehow in the code. The issue, is that then this object will cause failure in later tests. Finding the test that is leaking the mock is a major pain.
If there was a method to list all existing mocks, then we could check at the end of every test that we have the expected number of mocks left.
Here is an exemple of incorrect test that could silently leak a mock that will leave for the remainder of the test suite
- (void)testHasToCallStopMockingManually_ifUsingPartialMockForObject_AND_StubbingSharedInstance {
__weak id weakRef;
@autoreleasepool {
id mock = [OCMockObject partialMockForObject: [singletonClass sharedInstance]];
weakRef = mock;
[[[mock stub] andReturn: mock] sharedInstance];
[[mock stub] dummyMethod];
}
// check that somehow the mock object was not dealloc'd and the stub is still in place
XCTAssertNotNil(weakRef);
XCTAssertEqualObjects(weakRef, [singletonClass sharedInstance]);
//in this case we have to call stop mocking manually
[weakRef stopMocking];
//check that indeed the mock has stopped
XCTAssertNotEqualObjects(weakRef, [singletonClass sharedInstance]);
}
The text was updated successfully, but these errors were encountered:
I'm hitting the same issue regularly. This makes tests unpredictable because they fail when run as part of a test suite but succeed individually. Making it easier to track down the cause of the leak would be super helpful.
We are running regularly into issues where a mock outlives the duration of a test because it gets retained somehow in the code. The issue, is that then this object will cause failure in later tests. Finding the test that is leaking the mock is a major pain.
If there was a method to list all existing mocks, then we could check at the end of every test that we have the expected number of mocks left.
Here is an exemple of incorrect test that could silently leak a mock that will leave for the remainder of the test suite
The text was updated successfully, but these errors were encountered: