2009-08-23 14 views
27

Ho recentemente iniziato a imparare Objective-C e scrivere i miei test utilizzando OCUnit fornito in dotazione con Xcode.BDD in Objective-C

Sono un programmatore di Ruby da molto tempo e sono abituato a RSpec e Cucumber: ottimi framework BDD.

Esiste un framework BDD decente da utilizzare in Objective-C? Mi manca il mio 'should's :)

risposta

17

C'è un progetto relativamente nuovo chiamato uispec che è stato ispirato dal test DSL di RSpec. L'esempio spec si presenta così:

#import "DescribeEmployeeAdmin.h" 
#import "SpecHelper.h" 

@implementation DescribeEmployeeAdmin 

-(void)before { 
    //login as default admin before each example 
    [SpecHelper loginAsAdmin]; 
} 

-(void)after { 
    //logout after each example 
    [SpecHelper logout]; 
} 

-(void)itShouldHaveDefaultUsers { 
    //Check that all default users are in list 
    [[app.tableView.label text:@"Larry Stooge"] should].exist; 
    [[app.tableView.label text:@"Curly Stooge"] should].exist; 
    [[app.tableView.label text:@"Moe Stooge"] should].exist; 
} 

-(void)itShouldAddAUser { 
    //Click the + button 
    [app.navigationButton touch]; 

    //Set the form fields. 
    //Also ".with" is optional so we here we can show the different syntax 
    [[app.textField.with placeholder:@"First Name"] setText:@"Brian"]; 
    [[app.textField.with placeholder:@"Last Name"] setText:@"Knorr"]; 
    [[app.textField.with placeholder:@"Email"] setText:@"[email protected]"]; 
    [[app.textField placeholder:@"Username"] setText:@"bkuser"]; 
    [[app.textField placeholder:@"Password"] setText:@"test"]; 
    [[app.textField placeholder:@"Confirm"] setText:@"test"]; 

    //Click the Save button 
    [[app.navigationButton.label text:@"Save"] touch]; 

    //Make sure the error alert view doesn't appear 
    [app timeout:1].alertView.should.not.exist; 

    //User list should now have a new entry 
    [[app.tableView.label text:@"Brian Knorr"] should].exist; 
} 

@end 

Tenete a mente che non ho mai usato, quindi c'è una possibilità che non si adatta alle vostre esigenze esattamente. Ma per lo meno, sarai in grado di usare il codice base come fonte d'ispirazione per scrivere il tuo framework di test.

+0

Il progetto sembra essere attivo e sembra quello di cui ho bisogno. Grazie! –

+0

Grazie, è molto bello. –

0

Non c'è nulla che ti impedisce di prefissare il tuo metodo di prova con Should. L'ho fatto con NUnit in C#.

+0

Quello che sto cercando è una sintassi specifica per la verifica. In Ruby sembra: method_under_test (args) .should be_valid –

+1

La persona che pone la domanda sta parlando di Objective-C e OCUnit, che si aspetta che i metodi di test inizino con "test" - è così che sa quali metodi sono testati metodi, poiché Objective-C non ha annotazioni come C# e Java do. –

8

Dai un'occhiata a come sono implementate le macro STAssert in OCUnit (SenTestingKit, incluso con Xcode).

Nel vostro pacco test di unità, è possibile implementare una categoria a NSObject per aggiungere metodi come un ipotetico -shouldBeValid che avrebbe poi chiamare lo stesso pass/fail macchinari che le STAssert macro fanno ora.

Nel caso in cui non siete intimamente familiare con il preprocessore C ...

Probabilmente dovrete anche utilizzare un #define per le macro di passare attraverso i giusti valori per __FILE__ e __LINE__ quando i test BDD fallire. Ad esempio, si potrebbe fare qualcosa di simile:

@interface NSObject (BehaviorDrivenDevelopment) 
- (void)shouldBeValidInFile:(const char *)file line:(int)line; 
@end 

#define shouldBeValid shouldBeValidInFile:__FILE__ line:__LINE__ 

In questo modo si potrebbe richiamare in questo modo:

[[someObject methodUnderTest:argument] shouldBeValid]; 

Il codice del compilatore vede sarà questo:

[[someObject methodUnderTest:argument] shouldBeValidInFile:__FILE__ line:__LINE__]; 

Le macro del preprocessore __FILE__ e __LINE__ si espandono nel file e nella riga corrente nel file sorgente del test.

In questo modo, quando si verifica un test non riuscito, è possibile passare le informazioni appropriate a SenTestingKit per inviarlo a Xcode. L'errore verrà visualizzato correttamente nella finestra Risultati build e facendo clic su di esso verrà visualizzata la posizione esatta dell'errore nei test.

14

Adam Milligan di Pivotal Labs ha creato un framework BDD per Objective-C chiamato Cedar destinato sia a Cocoa che a Cocoa Touch. Usa i blocchi in modo simile a RSpec. Ecco una specifica esempio:

SPEC_BEGIN(FooSpecs) 

sharedExamplesFor(@"a similarly-behaving thing", ^(NSDictionary *context) { 
    it(@"should do something common", ^{ 
     ... 
    }); 
}); 

NSDictionary *context = [NSDictionary dictionary]; 

describe(@"Something that shares behavior", ^{ 
    itShouldBehaveLike(@"a similarly-behaving thing", context); 
}); 

describe(@"Something else that shares behavior", ^{ 
    itShouldBehaveLike(@"a similarly-behaving thing", context); 
}); 

SPEC_END 
21

sto usando Kiwi Library rapida per integrare, funziona abbastanza bene.

Da their github:

describe(@"Team", ^{ 
    context(@"when newly created", ^{ 
     it(@"should have a name", ^{ 
      id team = [Team team]; 
      [[team.name should] equal:@"Black Hawks"]; 
     }); 

     it(@"should have 11 players", ^{ 
      id team = [Team team]; 
      [[[team should] have:11] players]; 
     }); 
    }); 
});