@@ -186,6 +186,16 @@ - (void)grey_invokeTest {
186186 andReplaceWithInstanceMethod: @selector (tearDown )];
187187 GREYFatalAssertWithMessage (swizzleSuccess, @" Cannot swizzle %@ tearDown" ,
188188 NSStringFromClass (selfClass));
189+
190+ IMP tearDownWithCompletionHandlerIMP =
191+ [self methodForSelector: @selector (grey_tearDownWithCompletionHandler: )];
192+ swizzleSuccess = [swizzler swizzleClass: selfClass
193+ addInstanceMethod: @selector (grey_tearDownWithCompletionHandler: )
194+ withImplementation: tearDownWithCompletionHandlerIMP
195+ andReplaceWithInstanceMethod: @selector (tearDownWithCompletionHandler: )];
196+ GREYFatalAssertWithMessage (
197+ swizzleSuccess,
198+ @" Cannot swizzle %@ tearDownWithCompletionHandler:" , NSStringFromClass (selfClass));
189199 [self grey_markSwizzled ];
190200 }
191201
@@ -275,7 +285,24 @@ - (void)grey_tearDown {
275285 return YES ;
276286 });
277287 INVOKE_ORIGINAL_IMP (void , @selector (grey_tearDown ));
278- [self grey_sendNotification: kGREYXCTestCaseInstanceDidTearDown ];
288+ }
289+
290+ /* *
291+ * A swizzled implementation for XCTestCase::tearDownWithCompletionHandler:.
292+ *
293+ * @remark These methods need to be added to each instance of XCTestCase because we don't expect
294+ * tests to invoke <tt> [super tearDownWithCompletionHandler:] </tt>.
295+ */
296+ - (void )grey_tearDownWithCompletionHandler : (void (^)(NSError *error))completion {
297+ __weak __typeof__ (self) weakSelf = self;
298+ INVOKE_ORIGINAL_IMP1 (void , @selector (grey_tearDownWithCompletionHandler: ), ^(NSError *error) {
299+ __typeof__ (self) strongSelf = weakSelf;
300+ GREYFatalAssertWithMessage (strongSelf, @" The test case should not have been deallocated." );
301+
302+ completion (error);
303+
304+ [strongSelf grey_sendNotification: kGREYXCTestCaseInstanceDidTearDown ];
305+ });
279306}
280307
281308/* *
0 commit comments