Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Unit Testing on iOS

Unit Testing on iOS

Talk about TDD and BDD on iOS held at the UIKonf

Tim Brückmann

May 02, 2013
Tweet

Other Decks in Programming

Transcript

  1. Executable Specification describe(@"NSNumber", ^{ context(@"when created with an integer value",

    ^{ __block NSNumber *numberUnderTest; beforeEach(^{ numberUnderTest = [NSNumber numberWithInteger:5]; }); it(@"should hold the correct value", ^{ [[theValue([numberUnderTest integerValue]) should] equal:theValue(5)]; }); }); });
  2. Better Architecture describe(@"my custom view with a text field", ^{

    context(@"when the value of the text field changes", ^{ it(@"should update the model object", ^{ // change value // model should be updated }); }); });
  3. Better Architecture describe(@"my custom view with a text field", ^{

    context(@"when the value of the text field changes", ^{ it(@"should update the model object", ^{ // change value // model should be updated }); }); }); MVC, anyone?
  4. Mocks // create mock object id carMock = [Car mock];

    // configure mock object [[carMock should] beMemberOfClass:[Car class]]; [[carMock should] receive:@selector(weight) andReturn:theValue(1500)]; // use mock object [garage parkCar:carMock]; [[theValue(garage.totalWeight) should] equal:theValue(1500)];
  5. Stubs // create real object id cruiser = [Cruiser cruiser];

    // stub method [[cruiser stubAndReturn:theValue(42.0f)] energyLevelInWarpCore:7]; // access stubbed method float energyLevel = [cruiser energyLevelInWarpCore:7]; [[theValue(energyLevel) should] equal:theValue(42.0f)];
  6. https://github.com/AliSoftware/OHHTTPStubs OHHTTPStubs Super easy network request stubbing [OHHTTPStubs shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request)

    { // Only stub requests to "*.json" files return [request.URL.absoluteString.lastPathComponent.pathExtension isEqualToString:@"json"]; } withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) { // Stub it with our "response.json" stub file return [OHHTTPStubsResponse responseWithFile:@"response.json" contentType:@"text/json" responseTime:2.0]; }];
  7. https://github.com/AliSoftware/OHHTTPStubs OHHTTPStubs Super easy network request stubbing [OHHTTPStubs shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request)

    { // Only stub requests to "*.json" files return [request.URL.absoluteString.lastPathComponent.pathExtension isEqualToString:@"json"]; } withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) { // Stub it with our "response.json" stub file return [OHHTTPStubsResponse responseWithFile:@"response.json" contentType:@"text/json" responseTime:2.0]; }];
  8. https://github.com/AliSoftware/OHHTTPStubs OHHTTPStubs Super easy network request stubbing [OHHTTPStubs shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request)

    { // Only stub requests to "*.json" files return [request.URL.absoluteString.lastPathComponent.pathExtension isEqualToString:@"json"]; } withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) { // Stub it with our "response.json" stub file return [OHHTTPStubsResponse responseWithFile:@"response.json" contentType:@"text/json" responseTime:2.0]; }];
  9. https://github.com/AliSoftware/OHHTTPStubs OHHTTPStubs Super easy network request stubbing [OHHTTPStubs shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request)

    { // Only stub requests to "*.json" files return [request.URL.absoluteString.lastPathComponent.pathExtension isEqualToString:@"json"]; } withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) { // Stub it with our "response.json" stub file return [OHHTTPStubsResponse responseWithFile:@"response.json" contentType:@"text/json" responseTime:2.0]; }]; OHHTTPStubsDownloadSpeedGPRS OHHTTPStubsDownloadSpeedEDGE OHHTTPStubsDownloadSpeed3G OHHTTPStubsDownloadSpeed3GPlus OHHTTPStubsDownloadSpeedWifi
  10. Async + Network context(@"when creating a task", ^{ beforeEach(^{ [OHHTTPStubs

    shouldStubRequestsPassingTest:^BOOL(NSURLRequest *request) { return [request.URL.lastPathComponent isEqualToString:@"tasks.json"]; } withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) { return [OHHTTPStubsResponse responseWithFile:@"todo_create.json" contentType:@"application/json" responseTime:1.0]; }]; }); it(@"should parse the response", ^{ task.text = @"new task"; [task create]; [[expectFutureValue(theValue(task.remoteIDValue)) shouldEventuallyBeforeTimingOutAfter(3)] equal:theValue(75704)]; }); });
  11. Core Data NSBundle *bundle = [NSBundle bundleForClass:klass]; NSManagedObjectModel *managedObjectModel =

    [NSManagedObjectModel mergedModelFromBundles:[NSArray arrayWithObject:bundle]]; NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel]; [persistentStoreCoordinator addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:nil options:nil error:nil]; NSManagedObjectContext *managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; managedObjectContext.persistentStoreCoordinator = persistentStoreCoordinator; https://github.com/rheinfabrik/RFTesting
  12. Core Data NSBundle *bundle = [NSBundle bundleForClass:klass]; NSManagedObjectModel *managedObjectModel =

    [NSManagedObjectModel mergedModelFromBundles:[NSArray arrayWithObject:bundle]]; NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel]; [persistentStoreCoordinator addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:nil options:nil error:nil]; NSManagedObjectContext *managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; managedObjectContext.persistentStoreCoordinator = persistentStoreCoordinator; https://github.com/rheinfabrik/RFTesting
  13. Core Data NSBundle *bundle = [NSBundle bundleForClass:klass]; NSManagedObjectModel *managedObjectModel =

    [NSManagedObjectModel mergedModelFromBundles:[NSArray arrayWithObject:bundle]]; NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel]; [persistentStoreCoordinator addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:nil options:nil error:nil]; NSManagedObjectContext *managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; managedObjectContext.persistentStoreCoordinator = persistentStoreCoordinator; https://github.com/rheinfabrik/RFTesting
  14. nibs/xibs // Use in initializers NSBundle *bundle = [NSBundle bundleForClass:[self

    class]]; // OS X — NSWindowController NSString *path = [bundle pathForResource:@"FooWindowController" ofType:@"nib"]; self = [self initWithWindowNibPath:path owner:self]; // iOS — UIViewController self = [super initWithNibName:@"FooViewController" bundle:bundle];