Skip to content

Latest commit

 

History

History
154 lines (104 loc) · 3.91 KB

README.md

File metadata and controls

154 lines (104 loc) · 3.91 KB

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

pod 'SHTestCaseAdditions'

Setup

Put this either in specific files or your project prefix file

#import "OCUnit+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];
  STAssertTrue(assertion, nil);
}

-(void)testSH_runLoopUntilTestPassesWithBlock_withTimeOut; {
  NSString * keyPath = @"sampleSet";
  __block BOOL didPass = NO;

  [self SH_addObserverForKeyPaths:@[keyPath].SH_toSet 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 mutableSetValueForKey:keyPath] addObject:@"Lol"];
  });

  
  [self SH_runLoopUntilTestPassesWithBlock:^BOOL{
    return didPass;
  } withTimeOut:5];
  
  
  STAssertTrue(didPass, nil);
  
}

-(void)testSH_performAsyncTestsWithinBlock_withTimeout; {
  NSString * keyPath = @"sampleArray";
  __block BOOL didPass = NO;
  [self SH_performAsyncTestsWithinBlock:^(BOOL *didFinish) {
    
    [self SH_addObserverForKeyPaths:@[keyPath].SH_toSet 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];
  
  STAssertTrue(didPass, nil);
  
}

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.