Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: list of mock objects #208

Open
otusweb opened this issue Jul 1, 2015 · 3 comments
Open

Feature request: list of mock objects #208

otusweb opened this issue Jul 1, 2015 · 3 comments

Comments

@otusweb
Copy link

otusweb commented Jul 1, 2015

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]);
}
@thomasdeniau
Copy link

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.

@pellet
Copy link

pellet commented Sep 2, 2016

+1
A static stopMockingAll method would be nice to chuck into the teardown method of a test suite.

@dmaclach
Copy link
Contributor

Reviving this from the dead, but check out #394 which I think does most of what you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants