2013-10-01 10 views
12

Ci scusiamo per la domanda noob, ma sto provando a verificare una connessione, ma non sono in grado di verificare il ritorno della richiesta, perché il test termina prima che il gestore di completamento abbia una possibilità di eseguire. Per ilustrate:Come verificare una sendAsynchronousRequest: su XCTest

-(void)testConnection 
{ 

    [[Conector sharedInstance] performAsynchronousRequestWithServerRequest:_srvRequest completionHandler:^(RequestAsynchronousStatus finishStatus, NSData *data) { 
     if (finishStatus == RequestAsynchronousOK){ 
      _data = data; 
      NSLog(@"Data OK"); 
     } 
    }]; 

    XCTAssertNotNil(_data, @"Data was nil"); 

} 

Quando provo a far valere, _data sarà sempre pari a zero, perché il gestore di completamento non è stato ancora eseguito. C'è un meccanismo per forzare il test ad attendere finché non ho una risposta dal metodo sendAsynchronousRequest :. Grazie in anticipo.

risposta

22

Questo sembra esattamente quello che vi serve:

XCAsyncTestCase: Asynchronous capace SenTestCase sottoclasse.

In sostanza, si dovrebbe scrivere il test come questo:

- (void)testConnection 
{ 
    [[Conector sharedInstance] performAsynchronousRequestWithServerRequest:_srvRequest completionHandler:^(RequestAsynchronousStatus finishStatus, NSData *data) { 
     if (finishStatus == RequestAsynchronousOK) 
     { 
      _data = data; 
      [self notify:XCTAsyncTestCaseStatusSucceeded]; 
      NSLog(@"Data OK"); 
     } 
    }]; 

    [self waitForTimeout:10]; 

    XCTAssertNotNil(_data, @"Data was nil"); 
} 

Avviso del waitForTimeout: chiamata e le chiamate notify:. 10 secondi dovrebbero essere sufficienti per il test, anche se dipenderebbe dalla richiesta stessa.

Si potrebbe anche ottenere più specifiche e attendere un certo status, in questo modo:

[self waitForStatus: XCTAsyncTestCaseStatusSucceeded timeout:10]; 

In questo modo, se la connessione non riesce a comunicare lo stato XCTAsyncTestCaseStatusSucceeded, la chiamata attesa sarà timeout e il test fallirebbe (come dovrebbe).

+0

Grazie uomo, che è esattamente quello che stavo cercando! –

+1

Potrebbe essere utile sapere che uno dei problemi noti menzionati nel readme XCAsyncTestCase è che "i test si bloccano quando vengono eseguiti con i bot di sviluppo XCode". Quindi, se stai usando i bot di sviluppo, potresti dare un'occhiata a 'xctest-additions' di iheartradio. – eremzeit

5

Ecco un'altra alternativa, XCAsyncTestCase in base alla versione GHUnit:

https://github.com/iheartradio/xctest-additions

L'uso è lo stesso, solo l'importazione e sottoclasse XCAsyncTestCase.

@implementation TestAsync 
- (void)testBlockSample 
{ 
    [self prepare]; 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(){ 
     sleep(1.0); 
     [self notify:kXCTUnitWaitStatusSuccess]; 
    }); 
    // Will wait for 2 seconds before expecting the test to have status success 
    // Potential statuses are: 
    // kXCTUnitWaitStatusUnknown, initial status 
    // kXCTUnitWaitStatusSuccess, indicates a successful callback 
    // kXCTUnitWaitStatusFailure, indicates a failed callback, e.g login operation failed 
    // kXCTUnitWaitStatusCancelled, indicates the operation was cancelled 
    [self waitForStatus:kXCTUnitWaitStatusSuccess timeout:2.0]; 
} 
Problemi correlati