-
Notifications
You must be signed in to change notification settings - Fork 609
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically stop all live mocks at the end of each test case/suite
If the user is using XCTest with OCMock, this registers a test observer that takes care of stopping all live mocks appropriately. For mocks that are created in +setUp, those will get stopped at the end of the suite. For mocks that are created in -setUp or in test cases themselves, those will get stopped at the end of the testcase. While these mocks are being stopped and testcases/suites are being torndown, messages sent to mocks are not going to trigger the exception about calling a mock after it has had stopMocking called on it. This allows objects that may refer to mocks in dealloc methods to be cleaned up in autoreleasepools or due to stopMocking being called without the mocks throwing exceptions. This should greatly simplify cleaning up mocks and remove a lot of potential leakage. It also makes sure that class mocks that mock class methods will not persist across tests.
- Loading branch information
Showing
7 changed files
with
251 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright (c) 2015-2020 Erik Doernenburg and contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
* not use these files except in compliance with the License. You may obtain | ||
* a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
#import <XCTest/XCTest.h> | ||
#import <OCMock/OCMock.h> | ||
|
||
#pragma mark Helper classes | ||
|
||
// Tests for mocks being stopped by the XCTestObserver that we register in OCMockObject. | ||
@interface OCMockObjectCleanupTests : XCTestCase | ||
@end | ||
|
||
@implementation OCMockObjectCleanupTests | ||
|
||
|
||
static id suiteMock; | ||
static id caseMock; | ||
|
||
+ (void)setUp | ||
{ | ||
suiteMock = [OCMockObject mockForClass:[NSString class]]; | ||
OCMStub([suiteMock intValue]).andReturn(42); | ||
caseMock = nil; | ||
} | ||
|
||
#pragma mark Tests suite mocks survive across test cases | ||
|
||
- (void)testSuiteMockWorksHere | ||
{ | ||
// Verify that a testSuite Mock made in +setUp doesn't get cleaned up until test suite is done. | ||
// By verifying in two test cases we know this is true (See testSuiteMockWorksAndHere). | ||
XCTAssertEqual([suiteMock intValue], 42); | ||
} | ||
|
||
- (void)testSuiteMockWorksAndHere | ||
{ | ||
// Verify that a testSuite Mock made in +setUp doesn't get cleaned up until test suite is done. | ||
// By verifying in two test cases we know this is true (See testSuiteMockWorksHere). | ||
XCTAssertEqual([suiteMock intValue], 42); | ||
} | ||
|
||
#pragma mark Tests case mocks get stopped across test cases | ||
|
||
- (void)setUpCaseMock | ||
{ | ||
caseMock = [OCMockObject mockForClass:[NSString class]]; | ||
} | ||
|
||
- (void)testCaseMockFailsEitherHere | ||
{ | ||
// Set up a mock here that should get cleaned up (but the global pointer will still be non-nil) | ||
// or test that the mock set up in testCaseMockFailsOrHere has had stop mocking called on it. | ||
if (!caseMock) | ||
{ | ||
[self setUpCaseMock]; | ||
} | ||
else | ||
{ | ||
XCTAssertThrows([caseMock stringValue], | ||
@"Expected a throw here because the caseMock set up in " | ||
@"testCaseMockFailsOrHere should have had stopMock called on it"); | ||
} | ||
} | ||
|
||
- (void)testCaseMockFailsOrHere | ||
{ | ||
// Set up a mock here that should get cleaned up (but the global pointer will still be non-nil) | ||
// or test that the mock set up in testCaseMockFailsOrHere has had stop mocking called on it. | ||
if (!caseMock) | ||
{ | ||
[self setUpCaseMock]; | ||
} | ||
else | ||
{ | ||
XCTAssertThrows([caseMock stringValue], | ||
@"Expected a throw here because the caseMock set up in " | ||
@"testCaseMockFailsEitherHere should have had stopMock called on it"); | ||
} | ||
} | ||
|
||
|
||
@end |