2015-03-19 11 views
11

La documentazione per XCTest waitForExpectationsWithTimeout: gestore :, afferma cheUtilizzando XCTest, come è possibile unire insieme più sequenze discrete di {expect -> wait}?

solo -waitForExpectationsWithTimeout: handler: può essere attivo in un dato momento, ma più sequenze discrete di {aspettative -> attesa} possono essere concatenati.

Tuttavia, non ho idea di come implementarlo, né posso trovare alcun esempio. Sto lavorando su una classe che deve prima trovare tutte le porte seriali disponibili, selezionare la porta corretta e quindi connettersi al dispositivo collegato a quella porta. Quindi, sto lavorando con almeno due aspettative, XCTestExpectation * expectationAllAvailablePorts e * expectationConnectedToDevice. Come collegherei quei due?

risposta

0

rapida

let expectation1 = //your definition 
let expectation2 = //your definition 

let result = XCTWaiter().wait(for: [expectation1, expectation2], timeout: 10, enforceOrder: true) 

if result == .completed { 
    //all expectations completed in order 
} 
+1

La tecnica ha finalmente raggiunto la documentazione. L'ho usato anche in Objective-C e funziona davvero. –

7

Faccio quanto segue e funziona.

expectation = [self expectationWithDescription:@"Testing Async Method Works!"]; 

[AsynClass method:parameter callbackFunction:^(BOOL callbackStatus, NSMutableArray* array) { 
    [expectation fulfil]; 
    // whatever 
}]; 

[self waitForExpectationsWithTimeout:5 handler:^(NSError *error) { 
    if (error) { 
     XCTFail(@"Expectation Failed with error: %@", error); 
    } 
    NSLog(@"expectation wait until handler finished "); 
}]; 

// do it again 

expectation = [self expectationWithDescription:@"Testing Async Method Works!"]; 

[CallBackClass method:parameter callbackFunction:^(BOOL callbackStatus, NSMutableArray* array) { 
    [expectation fulfil]; 
    // whatever 
}]; 

[self waitForExpectationsWithTimeout:5 handler:^(NSError *error) { 
    if (error) { 
     XCTFail(@"Expectation Failed with error: %@", error); 
    } 
    NSLog(@"expectation wait until handler finished "); 
}]; 
3

Assegnare la mia aspettativa a una variabile debole ha funzionato per me.

+0

Questo ha fatto il trucco !!!! Grazie per aver menzionato la variabile debole! –

+0

* Attenzione: * Il link "qui" sopra potrebbe non funzionare più. Invece di una pagina su variabili deboli, ora presenta una pagina che "richiede" l'installazione di "Flash Player Pro". Facendo clic sul pulsante [x] sul sito Web per "chiudere" l'avviso scaricato un file .dmg impreciso sul mio Mac. Non l'ho aperto WMMV. –

0

Questo sembra funzionare anche per me in Swift 3.0.

let spyDelegate = SpyDelegate() 
var asyncExpectation = expectation(description: "firstExpectation") 
spyDelegate.asyncExpectation = asyncExpectation 
let testee = MyClassToTest(delegate: spyDelegate) 
testee.myFunction() //asyncExpectation.fulfill() happens here, implemented in SpyDelegate 

waitForExpectations(timeout: 30.0) { (error: Error?) in 
    if let error = error { 
     XCTFail("error: \(error)") 
    } 
} 

asyncExpectation = expectation(description: "secoundExpectation") 
spyDelegate.asyncExpectation = asyncExpectation 
testee.delegate = spyDelegate 
testee.myOtherFunction() //asyncExpectation.fulfill() happens here, implemented in SpyDelegate 

waitForExpectations(timeout: 30.0) { (error: Error?) in 
    if let error = error { 
     XCTFail("error: \(error)") 
    } 
} 
Problemi correlati