Share
Embed
Luis Solano's Slides at NSSpain 2013
NSSpain is an iOS & OSX developers conference in Spain.
More info: http://nsspain.com
Luis Ascorbe
September 19, 2013
Transcript
127 slides
-
Consuming Web APIs, the TDD way Luis Solano @luisobo My
name is Luis Solano I work at Pixable, a startup based in NYC leading the iOS team. You can find me on twitter and github. My handler is “luisobo” -
Good practices for networking code - How to make it
more maintainable - Unlike most material on TDD, which only covers basic examples. - Principles that you can apply to any scenario in test-driven development. -
Good practices for networking code Tackle real life testing scenarios
- How to make it more maintainable - Unlike most material on TDD, which only covers basic examples. - Principles that you can apply to any scenario in test-driven development. -
Good practices for networking code Tackle real life testing scenarios
Underlaying principles of TDD - How to make it more maintainable - Unlike most material on TDD, which only covers basic examples. - Principles that you can apply to any scenario in test-driven development. -
Good practices for networking code Tackle real life testing scenarios
Underlaying principles of TDD Better user experience - How to make it more maintainable - Unlike most material on TDD, which only covers basic examples. - Principles that you can apply to any scenario in test-driven development. -
- Less loading time for our users - Less errors
and more meaningful ones. - Potentially less app updates -
- Less loading time for our users - Less errors
and more meaningful ones. - Potentially less app updates -
- Less loading time for our users - Less errors
and more meaningful ones. - Potentially less app updates -
API Client design - Let’s talk about some ideas that
our API clients sound embrace -
Forward compatibility - Ability for the API to evolve without
breaking existing clients - Consumers of the API enable this. -
@interface LSBUser : NSObject @property (nonatomic, strong) NSString *firstName; @property
(nonatomic, strong) NSString *lastName; @property (nonatomic, strong) NSString *city; // ... @end -
LSBUser *user = [[LSBUser alloc] init]; for (NSString *key in
JSON) { [user setValue:JSON[key] forKey:key]; } -
{ "user": { "firstName": "John", "lastName": "Appleseed", "city": "Valencia", //
... } } "newField": "something" -
NSUnknownKeyException "[<LSBUser 0x75837a0> setValueɿ forUndefinedKeyɿ]ɿ this class is not key
value coding-compliant for the key newField." raised -
{ "post":{ "links":[{ "rel":"self", "href":"http://blog/post/4711" },{ "rel":"author", "href":"http://author/uri" }], "title":"foobar",
"content":"A long article here…" } } -
require 'sinatra' require 'json' get "/dogs.json" do content_type :json {
dogs: [{ name: "perro", color: "brown" },{ name: "tomas", color: "black" }] }.to_json end -
it(@"retrieves dogs", ^{ }); [[LSBDogCare new] allDogs:^(NSArray *dogs) { }
failure:^(NSError *error) { }]; -
it(@"retrieves dogs", ^{ }); [[LSBDogCare new] allDogs:^(NSArray *dogs) { }
failure:^(NSError *error) { }]; __block NSArray *capturedDogs = nil; capturedDogs = dogs; -
it(@"retrieves dogs", ^{ }); [[LSBDogCare new] allDogs:^(NSArray *dogs) { }
failure:^(NSError *error) { }]; [[expectFutureValue(capturedDogs) shouldEventually] equal:@[@{@"name":@"perro",@"color":@"brown"}, @{@"name":@"tomas",@"color":@"black"} ]]; __block NSArray *capturedDogs = nil; capturedDogs = dogs; -
it(@"retrieves dogs", ^{ }); [[LSBDogCare new] allDogs:^(NSArray *dogs) { }
failure:^(NSError *error) { }]; [[expectFutureValue(capturedDogs) shouldEventually] equal:@[@{@"name":@"perro",@"color":@"brown"}, @{@"name":@"tomas",@"color":@"black"} ]]; __block NSArray *capturedDogs = nil; capturedDogs = dogs; -
@implementation LSBDogCare - (void)allDogs:(void(^)(NSArray *dogs))success failure:(void(^)(NSError *error))failure { } @end
NSURL *url = [NSURL URLWithString:@"http://localhost:4567/dogs.json"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; -
@implementation LSBDogCare - (void)allDogs:(void(^)(NSArray *dogs))success failure:(void(^)(NSError *error))failure { } @end
NSURL *url = [NSURL URLWithString:@"http://localhost:4567/dogs.json"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; AFJSONRequestOperation *op; op = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSDictionary *JSON) { } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *requestError, id JSON) { }]; -
@implementation LSBDogCare - (void)allDogs:(void(^)(NSArray *dogs))success failure:(void(^)(NSError *error))failure { } @end
NSURL *url = [NSURL URLWithString:@"http://localhost:4567/dogs.json"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; AFJSONRequestOperation *op; op = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSDictionary *JSON) { } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *requestError, id JSON) { }]; success(JSON[@"dogs"]); -
@implementation LSBDogCare - (void)allDogs:(void(^)(NSArray *dogs))success failure:(void(^)(NSError *error))failure { } @end
NSURL *url = [NSURL URLWithString:@"http://localhost:4567/dogs.json"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; AFJSONRequestOperation *op; op = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSDictionary *JSON) { } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *requestError, id JSON) { }]; success(JSON[@"dogs"]); [op start]; -
@implementation LSBDogCare - (void)allDogs:(void(^)(NSArray *dogs))success failure:(void(^)(NSError *error))failure { } @end
NSURL *url = [NSURL URLWithString:@"http://localhost:4567/dogs.json"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; AFJSONRequestOperation *op; op = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSDictionary *JSON) { } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *requestError, id JSON) { }]; success(JSON[@"dogs"]); [op start]; -
it(@"retrieves dogs", ^{ __block NSArray *capturedDogs = nil; [[LSBDogCare new]
allDogs:^(NSArray *dogs) { capturedDogs = dogs; } failure:^(NSError *error) { }]; [[expectFutureValue(capturedDogs) shouldEventually] equal:@[@{@"name":@"perro",@"color":@"brown"}, @{@"name":@"tomas",@"color":@"black"} ]]; }); BAM! We have our first test Raise you hand if you see any problems with this test What is wrong with it? It’s hitting the real network: undeterministic, slow dependency Real life test case Principle TDD: isolate code from undeterministic and slow depencencies. -
Isolate tests from undeterministic and slow depencencies. This applies to
databases, file system, global state, etc -
it(@"retrieves dogs", ^{ __block NSArray *capturedDogs = nil; [[LSBDogCare new]
allDogs:^(NSArray *dogs) { capturedDogs = dogs; } failure:^(NSError *error) { }]; [[expectFutureValue(capturedDogs) shouldEventually] equal:@[@{@"name":@"perro",@"color":@"brown"}]]; }); BAM! No network! No it’s deterministic, and fast. Raise you hand if you see any problems with this test. What is wrong with it? -
it(@"retrieves dogs", ^{ __block NSArray *capturedDogs = nil; [[LSBDogCare new]
allDogs:^(NSArray *dogs) { capturedDogs = dogs; } failure:^(NSError *error) { }]; [[expectFutureValue(capturedDogs) shouldEventually] equal:@[@{@"name":@"perro",@"color":@"brown"}]]; }); AFJSONRequestOperation *mockOp = [AFJSONRequestOperation mock]; BAM! No network! No it’s deterministic, and fast. Raise you hand if you see any problems with this test. What is wrong with it? -
it(@"retrieves dogs", ^{ __block NSArray *capturedDogs = nil; [[LSBDogCare new]
allDogs:^(NSArray *dogs) { capturedDogs = dogs; } failure:^(NSError *error) { }]; [[expectFutureValue(capturedDogs) shouldEventually] equal:@[@{@"name":@"perro",@"color":@"brown"}]]; }); AFJSONRequestOperation *mockOp = [AFJSONRequestOperation mock]; [AFJSONRequestOperation stub:@selector(JSONRequestOperationWithRequest:success:failure:) withBlock:^id(NSArray *params) { return mockOp; }]; BAM! No network! No it’s deterministic, and fast. Raise you hand if you see any problems with this test. What is wrong with it? -
it(@"retrieves dogs", ^{ __block NSArray *capturedDogs = nil; [[LSBDogCare new]
allDogs:^(NSArray *dogs) { capturedDogs = dogs; } failure:^(NSError *error) { }]; [[expectFutureValue(capturedDogs) shouldEventually] equal:@[@{@"name":@"perro",@"color":@"brown"}]]; }); AFJSONRequestOperation *mockOp = [AFJSONRequestOperation mock]; [AFJSONRequestOperation stub:@selector(JSONRequestOperationWithRequest:success:failure:) withBlock:^id(NSArray *params) { return mockOp; }]; __block NSURLRequest *capturedRequest; __block void (^capturedSuccess)(NSURLRequest *request, NSHTTPURLResponse *response, id JSON); BAM! No network! No it’s deterministic, and fast. Raise you hand if you see any problems with this test. What is wrong with it? -
it(@"retrieves dogs", ^{ __block NSArray *capturedDogs = nil; [[LSBDogCare new]
allDogs:^(NSArray *dogs) { capturedDogs = dogs; } failure:^(NSError *error) { }]; [[expectFutureValue(capturedDogs) shouldEventually] equal:@[@{@"name":@"perro",@"color":@"brown"}]]; }); AFJSONRequestOperation *mockOp = [AFJSONRequestOperation mock]; [AFJSONRequestOperation stub:@selector(JSONRequestOperationWithRequest:success:failure:) withBlock:^id(NSArray *params) { return mockOp; }]; __block NSURLRequest *capturedRequest; __block void (^capturedSuccess)(NSURLRequest *request, NSHTTPURLResponse *response, id JSON); capturedRequest = params[0]; capturedSuccess = params[1]; BAM! No network! No it’s deterministic, and fast. Raise you hand if you see any problems with this test. What is wrong with it? -
it(@"retrieves dogs", ^{ __block NSArray *capturedDogs = nil; [[LSBDogCare new]
allDogs:^(NSArray *dogs) { capturedDogs = dogs; } failure:^(NSError *error) { }]; [[expectFutureValue(capturedDogs) shouldEventually] equal:@[@{@"name":@"perro",@"color":@"brown"}]]; }); AFJSONRequestOperation *mockOp = [AFJSONRequestOperation mock]; [AFJSONRequestOperation stub:@selector(JSONRequestOperationWithRequest:success:failure:) withBlock:^id(NSArray *params) { return mockOp; }]; [mockOp stub:@selector(start) withBlock:^id(NSArray *params) { capturedSuccess(capturedRequest, [NSHTTPURLResponse mock], @{@"dogs":@[@{@"name":@"perro",@"color":@"brown"}]}); return nil; }]; __block NSURLRequest *capturedRequest; __block void (^capturedSuccess)(NSURLRequest *request, NSHTTPURLResponse *response, id JSON); capturedRequest = params[0]; capturedSuccess = params[1]; BAM! No network! No it’s deterministic, and fast. Raise you hand if you see any problems with this test. What is wrong with it? -
it(@"retrieves dogs", ^{ __block NSArray *capturedDogs = nil; [[LSBDogCare new]
allDogs:^(NSArray *dogs) { capturedDogs = dogs; } failure:^(NSError *error) { }]; [[expectFutureValue(capturedDogs) shouldEventually] equal:@[@{@"name":@"perro",@"color":@"brown"}]]; }); AFJSONRequestOperation *mockOp = [AFJSONRequestOperation mock]; [AFJSONRequestOperation stub:@selector(JSONRequestOperationWithRequest:success:failure:) withBlock:^id(NSArray *params) { return mockOp; }]; [mockOp stub:@selector(start) withBlock:^id(NSArray *params) { capturedSuccess(capturedRequest, [NSHTTPURLResponse mock], @{@"dogs":@[@{@"name":@"perro",@"color":@"brown"}]}); return nil; }]; __block NSURLRequest *capturedRequest; __block void (^capturedSuccess)(NSURLRequest *request, NSHTTPURLResponse *response, id JSON); capturedRequest = params[0]; capturedSuccess = params[1]; BAM! No network! No it’s deterministic, and fast. Raise you hand if you see any problems with this test. What is wrong with it? -
it(@"retrieves dogs", ^{ __block NSArray *capturedDogs = nil; [[LSBDogCare new]
allDogs:^(NSArray *dogs) { capturedDogs = dogs; } failure:^(NSError *error) { }]; [[expectFutureValue(capturedDogs) shouldEventually] equal:@[@{@"name":@"perro",@"color":@"brown"}]]; }); AFJSONRequestOperation *mockOp = [AFJSONRequestOperation mock]; [AFJSONRequestOperation stub:@selector(JSONRequestOperationWithRequest:success:failure:) withBlock:^id(NSArray *params) { return mockOp; }]; [mockOp stub:@selector(start) withBlock:^id(NSArray *params) { capturedSuccess(capturedRequest, [NSHTTPURLResponse mock], @{@"dogs":@[@{@"name":@"perro",@"color":@"brown"}]}); return nil; }]; __block NSURLRequest *capturedRequest; __block void (^capturedSuccess)(NSURLRequest *request, NSHTTPURLResponse *response, id JSON); capturedRequest = params[0]; capturedSuccess = params[1]; The problem is that the test it’s coupled with the implementation details. Let’s see what’s wrong with that. -
Test Implementation Refactor What I actually do What actually happens
Definition of refactoring. -
Test Implementation Refactor Change a test What I actually do
What actually happens Definition of refactoring. -
Test Implementation Refactor Change a test Change implementation What I
actually do What actually happens Definition of refactoring. -
Test Implementation Refactor Change a test Change implementation Shit I
forgot to stub that What I actually do What actually happens Definition of refactoring. -
it(@"retrieves dogs", ^{ __block NSArray *capturedDogs = nil; [[LSBDogCare new]
allDogs:^(NSArray *dogs) { capturedDogs = dogs; } failure:^(NSError *error) { }]; [[expectFutureValue(capturedDogs) shouldEventually] equal:@[@{@"name":@"perro",@"color":@"brown"}]]; }); AFJSONRequestOperation *mockOp = [AFJSONRequestOperation mock]; [AFJSONRequestOperation stub:@selector(JSONRequestOperationWithRequest:success:failure:) withBlock:^id(NSArray *params) { return mockOp; }]; [mockOp stub:@selector(start) withBlock:^id(NSArray *params) { capturedSuccess(capturedRequest, [NSHTTPURLResponse mock], @{@"dogs":@[@{@"name":@"perro",@"color":@"brown"}]}); return nil; }]; __block NSURLRequest *capturedRequest; __block void (^capturedSuccess)(NSURLRequest *request, NSHTTPURLResponse *response, id JSON); capturedRequest = params[0]; capturedSuccess = params[1]; Then, how can we test this? -
it(@"retrieves dogs", ^{ __block NSArray *capturedDogs = nil; [[LSBDogCare new]
allDogs:^(NSArray *dogs) { capturedDogs = dogs; } failure:^(NSError *error) { }]; [[expectFutureValue(capturedDogs) shouldEventually] equal:@[@{@"name":@"perro",@"color":@"brown"}, @{@"name":@"tomas",@"color":@"black"}]]; }); -
it(@"retrieves dogs", ^{ __block NSArray *capturedDogs = nil; [[LSBDogCare new]
allDogs:^(NSArray *dogs) { capturedDogs = dogs; } failure:^(NSError *error) { }]; [[expectFutureValue(capturedDogs) shouldEventually] equal:@[@{@"name":@"perro",@"color":@"brown"}, @{@"name":@"tomas",@"color":@"black"}]]; }); stubRequest(@"GET", @"http://localhost:4567/dogs.json") -
it(@"retrieves dogs", ^{ __block NSArray *capturedDogs = nil; [[LSBDogCare new]
allDogs:^(NSArray *dogs) { capturedDogs = dogs; } failure:^(NSError *error) { }]; [[expectFutureValue(capturedDogs) shouldEventually] equal:@[@{@"name":@"perro",@"color":@"brown"}, @{@"name":@"tomas",@"color":@"black"}]]; }); stubRequest(@"GET", @"http://localhost:4567/dogs.json") .andReturn(200) -
it(@"retrieves dogs", ^{ __block NSArray *capturedDogs = nil; [[LSBDogCare new]
allDogs:^(NSArray *dogs) { capturedDogs = dogs; } failure:^(NSError *error) { }]; [[expectFutureValue(capturedDogs) shouldEventually] equal:@[@{@"name":@"perro",@"color":@"brown"}, @{@"name":@"tomas",@"color":@"black"}]]; }); stubRequest(@"GET", @"http://localhost:4567/dogs.json") .andReturn(200) .withHeaders(@{ @"Content-Type": @"application/json;charset=utf-8" }) -
it(@"retrieves dogs", ^{ __block NSArray *capturedDogs = nil; [[LSBDogCare new]
allDogs:^(NSArray *dogs) { capturedDogs = dogs; } failure:^(NSError *error) { }]; [[expectFutureValue(capturedDogs) shouldEventually] equal:@[@{@"name":@"perro",@"color":@"brown"}, @{@"name":@"tomas",@"color":@"black"}]]; }); stubRequest(@"GET", @"http://localhost:4567/dogs.json") .andReturn(200) .withHeaders(@{ @"Content-Type": @"application/json;charset=utf-8" }) .withBody([@{@"dogs":@[ @{@"name":@"perro",@"color":@"brown"}, @{@"name":@"tomas",@"color":@"black"}] } JSONString]); -
it(@"retrieves dogs", ^{ __block NSArray *capturedDogs = nil; [[LSBDogCare new]
allDogs:^(NSArray *dogs) { capturedDogs = dogs; } failure:^(NSError *error) { }]; [[expectFutureValue(capturedDogs) shouldEventually] equal:@[@{@"name":@"perro",@"color":@"brown"}, @{@"name":@"tomas",@"color":@"black"}]]; }); stubRequest(@"GET", @"http://localhost:4567/dogs.json") .andReturn(200) .withHeaders(@{ @"Content-Type": @"application/json;charset=utf-8" }) .withBody([@{@"dogs":@[ @{@"name":@"perro",@"color":@"brown"}, @{@"name":@"tomas",@"color":@"black"}] } JSONString]); -
Don’t couple test with implementation details Don’t test private methods
Test external behavior of your modules Otherwise you will live in a tested trap. You won’t be able to leverage your tests to refactor. -
context(@"when retrieving dogs fail because access was revoked", ^{ it(@"reports
an error", ^{ }); }); -
context(@"when retrieving dogs fail because access was revoked", ^{ it(@"reports
an error", ^{ }); }); __block NSError *capturedError; [[LSBDogCare new] allDogs:^(NSArray *dogs) { } failure:^(NSError *error) { capturedError = error; }]; -
context(@"when retrieving dogs fail because access was revoked", ^{ it(@"reports
an error", ^{ }); }); __block NSError *capturedError; [[LSBDogCare new] allDogs:^(NSArray *dogs) { } failure:^(NSError *error) { capturedError = error; }]; [[expectFutureValue(capturedError) shouldEventually] equal:[NSError errorWithDomain:@"com.luisobo.dogcare" code:23 userInfo:@{ NSLocalizedDescriptionKey: @"uh uh uh, you didn't say the magic word" }]]; -
context(@"when retrieving dogs fail because access was revoked", ^{ it(@"reports
an error", ^{ }); }); __block NSError *capturedError; [[LSBDogCare new] allDogs:^(NSArray *dogs) { } failure:^(NSError *error) { capturedError = error; }]; [[expectFutureValue(capturedError) shouldEventually] equal:[NSError errorWithDomain:@"com.luisobo.dogcare" code:23 userInfo:@{ NSLocalizedDescriptionKey: @"uh uh uh, you didn't say the magic word" }]]; stubRequest(@"GET", @"http://localhost:4567/dogs.json") .andReturn(401); -
context(@"when retrieving dogs fail because access was revoked", ^{ it(@"reports
an error", ^{ }); }); __block NSError *capturedError; [[LSBDogCare new] allDogs:^(NSArray *dogs) { } failure:^(NSError *error) { capturedError = error; }]; [[expectFutureValue(capturedError) shouldEventually] equal:[NSError errorWithDomain:@"com.luisobo.dogcare" code:23 userInfo:@{ NSLocalizedDescriptionKey: @"uh uh uh, you didn't say the magic word" }]]; stubRequest(@"GET", @"http://localhost:4567/dogs.json") .andReturn(401); -
- (void)allDogs:(void(^)(NSArray *dogs))success failure:(void(^)(NSError *error))failure { // ... op =
[AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSDictionary *JSON) { success(JSON[@"dogs"]); } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { }]; [op start]; } -
- (void)allDogs:(void(^)(NSArray *dogs))success failure:(void(^)(NSError *error))failure { // ... op =
[AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSDictionary *JSON) { success(JSON[@"dogs"]); } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { }]; [op start]; } if (response.statusCode == 401) { error = [NSError errorWithDomain:@"com.luisobo.dogcare" code:23 userInfo:@{ NSLocalizedDescriptionKey: @"uh uh uh, you didn't say the magic word" }]; } failure(error); -
- (void)allDogs:(void(^)(NSArray *dogs))success failure:(void(^)(NSError *error))failure { // ... op =
[AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSDictionary *JSON) { success(JSON[@"dogs"]); } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { }]; [op start]; } if (response.statusCode == 401) { error = [NSError errorWithDomain:@"com.luisobo.dogcare" code:23 userInfo:@{ NSLocalizedDescriptionKey: @"uh uh uh, you didn't say the magic word" }]; } failure(error); -
context(@"when the request fails because there is no internet", ^{
it(@"reports an error", ^{ }); }); -
context(@"when the request fails because there is no internet", ^{
it(@"reports an error", ^{ }); }); __block NSError *capturedError; [[LSBDogCare new] allDogs:^(NSArray *dogs) { } failure:^(NSError *error) { capturedError = error; }]; -
context(@"when the request fails because there is no internet", ^{
it(@"reports an error", ^{ }); }); __block NSError *capturedError; [[LSBDogCare new] allDogs:^(NSArray *dogs) { } failure:^(NSError *error) { capturedError = error; }]; [[expectFutureValue(capturedError) shouldEventually] equal:[NSError errorWithDomain:@"com.luisobo.dogcare" code:446 userInfo:@{ NSLocalizedDescriptionKey: @"eeeer, check your internet dumbass" }]]; -
context(@"when the request fails because there is no internet", ^{
it(@"reports an error", ^{ }); }); __block NSError *capturedError; [[LSBDogCare new] allDogs:^(NSArray *dogs) { } failure:^(NSError *error) { capturedError = error; }]; [[expectFutureValue(capturedError) shouldEventually] equal:[NSError errorWithDomain:@"com.luisobo.dogcare" code:446 userInfo:@{ NSLocalizedDescriptionKey: @"eeeer, check your internet dumbass" }]]; stubRequest(@"GET", @"http://localhost:4567/dogs.json") .andFailWithError([NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCannotConnectToHost userInfo:nil]); -
context(@"when the request fails because there is no internet", ^{
it(@"reports an error", ^{ }); }); __block NSError *capturedError; [[LSBDogCare new] allDogs:^(NSArray *dogs) { } failure:^(NSError *error) { capturedError = error; }]; [[expectFutureValue(capturedError) shouldEventually] equal:[NSError errorWithDomain:@"com.luisobo.dogcare" code:446 userInfo:@{ NSLocalizedDescriptionKey: @"eeeer, check your internet dumbass" }]]; stubRequest(@"GET", @"http://localhost:4567/dogs.json") .andFailWithError([NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCannotConnectToHost userInfo:nil]); -
context(@"when the request fails because there is no internet", ^{
it(@"reports an error", ^{ }); }); __block NSError *capturedError; [[LSBDogCare new] allDogs:^(NSArray *dogs) { } failure:^(NSError *error) { capturedError = error; }]; [[expectFutureValue(capturedError) shouldEventually] equal:[NSError errorWithDomain:@"com.luisobo.dogcare" code:446 userInfo:@{ NSLocalizedDescriptionKey: @"eeeer, check your internet dumbass" }]]; stubRequest(@"GET", @"http://localhost:4567/dogs.json") .andFailWithError([NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCannotConnectToHost userInfo:nil]); -
Test Implementation Refactor Nocilla enables proper TDD. It decouples tests
from implementation. -
Error handling The operation could not be completed . (com.facebook.sdk
error 2) Often neglected -
1. Developer screw-ups Developer mistake - Passed nil as an
argument. - Fail fast and often - Beyond that point, the behavior is not defined. - Assert until it bleeds - Crash: BAM! IN YO FACE! -
1. Developer screw-ups 2. Everything else Developer mistake - Passed
nil as an argument. - Fail fast and often - Beyond that point, the behavior is not defined. - Assert until it bleeds - Crash: BAM! IN YO FACE! -
Group errors based on how the client is going to
recover from them -
1. Automatic recoverable errors (e.g. reauthorize) 2. User recoverable errors
(e.g. validation, ask for more permissions) -
1. Automatic recoverable errors (e.g. reauthorize) 2. User recoverable errors
(e.g. validation, ask for more permissions) 3. Known non-recoverable errors. (e.g. server is down, rate limiting) -
1. Automatic recoverable errors (e.g. reauthorize) 2. User recoverable errors
(e.g. validation, ask for more permissions) 3. Known non-recoverable errors. (e.g. server is down, rate limiting) 4. “How the fu*k did I get here?” errors -
NSError = domain + code Tuple to uniquely identify an
error. Don’t check for error codes. You want to know how to react to an error Ask the error for the recovery strategy -
@interface NSError (FacebookAPI) @end Let’s say that we are implementing
the Facebook SDK used privately forms an error from a response -
@interface NSError (FacebookAPI) @end + (instancetype)facebookAPIErrorWithResponse:(NSDictionary *)response; Let’s say that
we are implementing the Facebook SDK used privately forms an error from a response -
@interface NSError (FacebookAPI) @end + (instancetype)facebookAPIErrorWithResponse:(NSDictionary *)response; - (BOOL)isFacebookAPIError; Let’s
say that we are implementing the Facebook SDK used privately forms an error from a response -
@interface NSError (FacebookAPI) @end + (instancetype)facebookAPIErrorWithResponse:(NSDictionary *)response; - (BOOL)isFacebookAPIError; -
(BOOL)shouldRecoverByReauthorizing; Let’s say that we are implementing the Facebook SDK used privately forms an error from a response -
@interface NSError (FacebookAPI) @end + (instancetype)facebookAPIErrorWithResponse:(NSDictionary *)response; - (BOOL)isFacebookAPIError; -
(BOOL)shouldRecoverByReauthorizing; - (BOOL)shouldRecoverByRequestingMorePermissions; Let’s say that we are implementing the Facebook SDK used privately forms an error from a response -
@interface NSError (FacebookAPI) @end + (instancetype)facebookAPIErrorWithResponse:(NSDictionary *)response; - (BOOL)isFacebookAPIError; -
(BOOL)shouldRecoverByReauthorizing; - (BOOL)shouldRecoverByRequestingMorePermissions; - (BOOL)shouldRecoverByNotifyingUser; Let’s say that we are implementing the Facebook SDK used privately forms an error from a response -
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { }
1. Automatic recoverable errors (reauthorize) 2. User recoverable errors (validation) -
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { }
NSError *facebookError = [NSError facebookAPIErrorWithResponse:JSON]; 1. Automatic recoverable errors (reauthorize) 2. User recoverable errors (validation) -
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { }
NSError *facebookError = [NSError facebookAPIErrorWithResponse:JSON]; if ([facebookError shouldRecoverByReauthorizing]) { // Reauthorize } 1. Automatic recoverable errors (reauthorize) 2. User recoverable errors (validation) -
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { }
NSError *facebookError = [NSError facebookAPIErrorWithResponse:JSON]; if ([facebookError shouldRecoverByReauthorizing]) { // Reauthorize } else if ([facebookError shouldRecoverByRequestingMorePermissions]) { // Request more permissions } 1. Automatic recoverable errors (reauthorize) 2. User recoverable errors (validation) -
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { }
NSError *facebookError = [NSError facebookAPIErrorWithResponse:JSON]; if ([facebookError shouldRecoverByReauthorizing]) { // Reauthorize } else if ([facebookError shouldRecoverByRequestingMorePermissions]) { // Request more permissions } else if ([facebookError shouldRecoverByNotifyingUser]) { } 1. Automatic recoverable errors (reauthorize) 2. User recoverable errors (validation) -
@interface NSError (FacebookSDK) + (instancetype)facebookSDKErrorWithError:(NSError *)error; - (BOOL)isFacebookSDKError; // ...
Same deal @end Higher level of abstraction Different domain User friendly message Use NSUnderlayingErrorKey -
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { }
NSError *facebookError = [NSError facebookAPIErrorWithResponse:JSON]; if ([facebookError shouldRecoverByReauthorizing]) { // Reauthorize } else if ([facebookError shouldRecoverByRequestingMorePermissions]) { // Request more permissions } else if ([facebookError shouldRecoverByNotifyingUser]) { } 3. Known non-recoverable errors. (e.g. server is down, rate limiting) 4. “How the fu*k did I get here?” errors log remotely - alternative to crash reports TIME? More slides? -
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { }
NSError *facebookError = [NSError facebookAPIErrorWithResponse:JSON]; if ([facebookError shouldRecoverByReauthorizing]) { // Reauthorize } else if ([facebookError shouldRecoverByRequestingMorePermissions]) { // Request more permissions } else if ([facebookError shouldRecoverByNotifyingUser]) { } failure([NSError facebookSDKErrorWithError:error]); 3. Known non-recoverable errors. (e.g. server is down, rate limiting) 4. “How the fu*k did I get here?” errors log remotely - alternative to crash reports TIME? More slides? -
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { }
NSError *facebookError = [NSError facebookAPIErrorWithResponse:JSON]; if ([facebookError shouldRecoverByReauthorizing]) { // Reauthorize } else if ([facebookError shouldRecoverByRequestingMorePermissions]) { // Request more permissions } else if ([facebookError shouldRecoverByNotifyingUser]) { } else { failure([NSError genericError]); } failure([NSError facebookSDKErrorWithError:error]); 3. Known non-recoverable errors. (e.g. server is down, rate limiting) 4. “How the fu*k did I get here?” errors log remotely - alternative to crash reports TIME? More slides? -
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { }
NSError *facebookError = [NSError facebookAPIErrorWithResponse:JSON]; if ([facebookError shouldRecoverByReauthorizing]) { // Reauthorize } else if ([facebookError shouldRecoverByRequestingMorePermissions]) { // Request more permissions } else if ([facebookError shouldRecoverByNotifyingUser]) { } else { failure([NSError genericError]); } failure([NSError facebookSDKErrorWithError:error]); // Ensure consistent state 3. Known non-recoverable errors. (e.g. server is down, rate limiting) 4. “How the fu*k did I get here?” errors log remotely - alternative to crash reports TIME? More slides? -
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { }
NSError *facebookError = [NSError facebookAPIErrorWithResponse:JSON]; if ([facebookError shouldRecoverByReauthorizing]) { // Reauthorize } else if ([facebookError shouldRecoverByRequestingMorePermissions]) { // Request more permissions } else if ([facebookError shouldRecoverByNotifyingUser]) { } else { failure([NSError genericError]); } failure([NSError facebookSDKErrorWithError:error]); // Ensure consistent state // Log facebookError in a remote service 3. Known non-recoverable errors. (e.g. server is down, rate limiting) 4. “How the fu*k did I get here?” errors log remotely - alternative to crash reports TIME? More slides? -
Build an abstraction on top of of an stateless wrapper
Take forward compatibility into account Recap -
Build an abstraction on top of of an stateless wrapper
Take forward compatibility into account Isolate test from undeterministic and slow dependencies. Recap -
Build an abstraction on top of of an stateless wrapper
Take forward compatibility into account Isolate test from undeterministic and slow dependencies. Don’t modify implementation and tests at the same time. Recap -
Build an abstraction on top of of an stateless wrapper
Take forward compatibility into account Isolate test from undeterministic and slow dependencies. Don’t modify implementation and tests at the same time. Don’t couple test with implementation details Recap -
Build an abstraction on top of of an stateless wrapper
Take forward compatibility into account Isolate test from undeterministic and slow dependencies. Don’t modify implementation and tests at the same time. Don’t couple test with implementation details Group errors by recover strategy Recap -
Build an abstraction on top of of an stateless wrapper
Take forward compatibility into account Isolate test from undeterministic and slow dependencies. Don’t modify implementation and tests at the same time. Don’t couple test with implementation details Group errors by recover strategy Recap