Skip to content

Feature request: list of mock objects #208

@otusweb

Description

@otusweb

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]);
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions