 						
 						expection.fulfill()
 						
-					}.error{ (error) in
+					}.catch{ (error) in
 						print(error)
 						XCTFail((error as NSError).description)
 				}
 		}
 		
-		waitForExpectationsWithTimeout(2, handler: nil)
+		waitForExpectations(timeout: 2, handler: nil)
 		
 	}
 	
 	func testGetVehicleState() {
 		
-		let path = NSBundle(forClass: self.dynamicType).pathForResource("MobileAccess", ofType: "json")!
-		let data = NSData(contentsOfFile: path)!
-		stub(uri(Endpoint.MobileAccess(vehicleID: 1234567890).path), builder: jsonData(data))
-		let path2 = NSBundle(forClass: self.dynamicType).pathForResource("ChargeState", ofType: "json")!
-		let data2 = NSData(contentsOfFile: path2)!
-		stub(uri(Endpoint.ChargeState(vehicleID: 1234567890).path), builder: jsonData(data2))
-		let path3 = NSBundle(forClass: self.dynamicType).pathForResource("ClimateSettings", ofType: "json")!
-		let data3 = NSData(contentsOfFile: path3)!
-		stub(uri(Endpoint.ClimateState(vehicleID: 1234567890).path), builder: jsonData(data3))
-		let path4 = NSBundle(forClass: self.dynamicType).pathForResource("DriveState", ofType: "json")!
-		let data4 = NSData(contentsOfFile: path4)!
-		stub(uri(Endpoint.DriveState(vehicleID: 1234567890).path), builder: jsonData(data4))
-		let path5 = NSBundle(forClass: self.dynamicType).pathForResource("GuiSettings", ofType: "json")!
-		let data5 = NSData(contentsOfFile: path5)!
-		stub(uri(Endpoint.GuiSettings(vehicleID: 1234567890).path), builder: jsonData(data5))
-		let path6 = NSBundle(forClass: self.dynamicType).pathForResource("VehicleState", ofType: "json")!
-		let data6 = NSData(contentsOfFile: path6)!
-		stub(uri(Endpoint.VehicleState(vehicleID: 1234567890).path), builder: jsonData(data6))
-		
-		let expection = expectationWithDescription("All Done")
+		let stubPath = OHPathForFile("MobileAccess.json", type(of: self))
+		_ = stub(condition: isPath(Endpoint.mobileAccess(vehicleID: 1234567890).path)) {
+			_ in
+			return fixture(filePath: stubPath!, headers: self.headers)
+		}
+		let stubPath2 = OHPathForFile("ChargeState.json", type(of: self))
+		_ = stub(condition: isPath(Endpoint.chargeState(vehicleID: 1234567890).path)) {
+			_ in
+			return fixture(filePath: stubPath2!, headers: self.headers)
+		}
+		let stubPath3 = OHPathForFile("ClimateSettings.json", type(of: self))
+		_ = stub(condition: isPath(Endpoint.climateState(vehicleID: 1234567890).path)) {
+			_ in
+			return fixture(filePath: stubPath3!, headers: self.headers)
+		}
+		let stubPath4 = OHPathForFile("DriveState.json", type(of: self))
+		_ = stub(condition: isPath(Endpoint.driveState(vehicleID: 1234567890).path)) {
+			_ in
+			return fixture(filePath: stubPath4!, headers: self.headers)
+		}
+		let stubPath5 = OHPathForFile("GuiSettings.json", type(of: self))
+		_ = stub(condition: isPath(Endpoint.guiSettings(vehicleID: 1234567890).path)) {
+			_ in
+			return fixture(filePath: stubPath5!, headers: self.headers)
+		}
+		let stubPath6 = OHPathForFile("VehicleState.json", type(of: self))
+		_ = stub(condition: isPath(Endpoint.vehicleState(vehicleID: 1234567890).path)) {
+			_ in
+			return fixture(filePath: stubPath6!, headers: self.headers)
+		}
+		
+		let expection = expectation(description: "All Done")
 		
 		let service = TeslaSwift()
 		service.useMockServer = true


import XCTest
@testable import TeslaSwift
import Mockingjay

class TeslaSwiftTests: XCTestCase {
	
	override func setUp() {
		super.setUp()
		// Put setup code here. This method is called before the invocation of each test method in the class.
		
		let path = NSBundle(forClass: self.dynamicType).pathForResource("Authentication", ofType: "json")!
		let data = NSData(contentsOfFile: path)!
		stub(uri(Endpoint.Authentication.path), builder: jsonData(data))
		
		let path2 = NSBundle(forClass: self.dynamicType).pathForResource("Vehicles", ofType: "json")!
		let data2 = NSData(contentsOfFile: path2)!
		stub(uri(Endpoint.Vehicles.path), builder: jsonData(data2))
	}
	
	override func tearDown() {
		// Put teardown code here. This method is called after the invocation of each test method in the class.
		super.tearDown()
	}
	
	func testAuthenticate() {
		
		let expection = expectationWithDescription("All Done")
		
		let service = TeslaSwift()
		service.useMockServer = true

		service.authenticate("user", password: "pass").then { (token) in
			service.getVehicles()
			}.then { (vehicles) in
				service.sendCommandToVehicle(vehicles[0], command: .openTrunk(options: options))
			}.then { (response) -> Void in
				
				XCTAssertEqual(response.result, false)
				XCTAssertEqual(response.reason, "Test OpenTrunk")
				
				expection.fulfill()
			}.catch { (error) in
				print(error)
				XCTFail((error as NSError).description)
		}
		
		waitForExpectations(timeout: 2, handler: nil)
	}
}
