Skip to content

JRG-Developer/SHTestCaseAdditions

 
 

Repository files navigation

SHTestCaseAdditions

Build Status Version Platform

Overview

Prefixed Asynchronous test helpers as a category on SenTestCase and XCTestCase

  • Swizzle and junk free
  • Prefixed selectors.
  • Works with both SentTest (Xcode 4) and XCTest (Xcode 5)
  • For iOS and Mac OS
  • Comes with tests and example.

Navigation

Installation

Default is for XCTest

pod 'SHTestCaseAdditions'
pod 'SHTestCaseAdditions/XCTest'

For SenTestKit

pod 'SHTestCaseAdditions/SenTestKit'

Setup

Put this either in specific files or your project prefix file

XCTestCase

#import <XCTestCase+SHTestCaseAdditions.h>

SenTestKit

#import <SenTestCase+SHTestCaseAdditions.h>

or

#import "SHTestCaseAdditions.h"

API

#pragma mark -
#pragma mark Block Definitions
typedef BOOL (^SHTestCaseConditional)();
typedef void (^SHTestCaseBlock)(BOOL *didFinish);

#pragma mark -
#pragma mark Helpers
-(void)SH_waitForTimeInterval:(NSTimeInterval)theTimeInterval;

-(void)SH_runLoopUntilTestPassesWithBlock:(SHTestCaseConditional)theBlock
                              withTimeOut:(NSTimeInterval)theTimeout;

-(void)SH_performAsyncTestsWithinBlock:(SHTestCaseBlock)theBlock
                           withTimeout:(NSTimeInterval)theTimeout;

@end

Usage

-(void)testSH_waitForTimeInterval; {
  __block BOOL assertion = NO;
  
  double delayInSeconds = 5.0;
  dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
  dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    assertion = YES;
  });
  
  [self SH_waitForTimeInterval:delayInSeconds];
  XCTAssertTrue(assertion);
}

-(void)testSH_runLoopUntilTestPassesWithBlock_withTimeOut; {
  NSString * keyPath   = @"sampleSet";
  __block BOOL didPass = NO;
  
  [self SH_addObserverForKeyPaths:@[keyPath] withOptions:0 block:^(id weakSelf, NSString *keyPath, NSDictionary *change) {
    didPass = YES;
  }];
  
  double delayInSeconds = 2;
  dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
  dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    [[self mutableArrayValueForKey:keyPath] addObject:@"Lol"];
  });
  
  
  [self SH_runLoopUntilTestPassesWithBlock:^BOOL{
    return didPass;
  } withTimeOut:5];
  
  
  XCTAssertTrue(didPass);
  
}

-(void)testSH_performAsyncTestsWithinBlock_withTimeout; {
  NSString * keyPath   = @"sampleArray";
  __block BOOL didPass = NO;
  
  [self SH_performAsyncTestsWithinBlock:^(BOOL *didFinish) {
    
    [self SH_addObserverForKeyPaths:@[keyPath] withOptions:0 block:^(id weakSelf, NSString *keyPath, NSDictionary *change) {
      didPass    = YES;
      *didFinish = YES;
    }];
    
    double delayInSeconds = 2;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
      [[self mutableArrayValueForKey:keyPath] addObject:@"Lol"];
    });
    
  } withTimeout:5];
  
  XCTAssertTrue(didPass);
  
  
  
}

Contact

If you end up using SHTestCaseAdditions in a project, I'd love to hear about it.

email: seivan.heidari@icloud.com
twitter: @seivanheidari

License

SHTestCaseAdditions is © 2013 Seivan and may be freely distributed under the MIT license. See the LICENSE.md file.

About

Prefixed category on SenTestCase and XCTestCase to help with asynchronous callbacks

Resources

License

Stars

Watchers

Forks

Packages

No packages published