//
//  Helper.swift
//  GithubSwift
//
//  Created by Khoa Pham on 29/03/16.
//  Copyright © 2016 Fantageek. All rights reserved.
//

import Foundation
import Quick
import Mockingjay
import RxSwift

class DummySpec: QuickSpec {}

struct Helper {
  static func read(fileName: String) -> NSData {
    let path = NSBundle(forClass: DummySpec().dynamicType).pathForResource(fileName, ofType: "json")!
    return NSData(contentsOfFile: path)!
  }
}

public extension XCTestCase {
  public func async(file: String = #file, line: UInt = #line, action: (XCTestExpectation) -> Void) {
    let expectation = self.expectationWithDescription("")
    
    action(expectation)
    
    self.waitForExpectationsWithTimeout(1) { error in
      if error != nil {
        self.recordFailureWithDescription("fail", inFile: file, atLine: line, expected: true)
      }
    }
  }
}

extension XCTest {
  func stubRedirect(url: NSURL, statusCode: Int, redirectURL: NSURL) {
    
    func matcher(request: NSURLRequest) -> Bool {
      guard request.URL?.scheme == url.scheme && request.URL?.path == url.path else { return false }

      return true
    }
    
    self.stub(matcher, builder: http(statusCode, headers: ["Location": redirectURL.absoluteString]))
  }
}

//
//  Helper.swift
//  GithubSwift
//
//  Created by Khoa Pham on 29/03/16.
//  Copyright © 2016 Fantageek. All rights reserved.
//

import Foundation
import Quick
import Mockingjay
import RxSwift

class DummySpec: QuickSpec {}

struct Helper {
  static func read(fileName: String) -> NSData {
    let path = NSBundle(forClass: DummySpec().dynamicType).pathForResource(fileName, ofType: "json")!
    return NSData(contentsOfFile: path)!
  }
}

public extension XCTestCase {
  public func async(action: (XCTestExpectation) -> Void) {
    let expectation = self.expectationWithDescription("")
    
    action(expectation)
    
    self.waitForExpectationsWithTimeout(1, handler: nil)
  }
}

extension XCTest {
  func stubRedirect(url: NSURL, statusCode: Int, redirectURL: NSURL) {
    
    func matcher(request: NSURLRequest) -> Bool {
      guard request.URL?.scheme == url.scheme && request.URL?.path == url.path else { return false }

      return true
    }
    
    self.stub(matcher, builder: http(statusCode, headers: ["Location": redirectURL.absoluteString]))
  }
}

--- 
+++ 
@@ -21,12 +21,16 @@
 }
 
 public extension XCTestCase {
-  public func async(action: (XCTestExpectation) -> Void) {
+  public func async(file: String = #file, line: UInt = #line, action: (XCTestExpectation) -> Void) {
     let expectation = self.expectationWithDescription("")
     
     action(expectation)
     
-    self.waitForExpectationsWithTimeout(1, handler: nil)
+    self.waitForExpectationsWithTimeout(1) { error in
+      if error != nil {
+        self.recordFailureWithDescription("fail", inFile: file, atLine: line, expected: true)
+      }
+    }
   }
 }
 
Refactor helper to include lines
