lundi 6 avril 2015

Test Unit Using Kiwi TDD Framework.

Have been struggling with writing a test unit for a method:



- (id<ISFModalMessageView>)modalMessageWithError:(NSError *)error
recoveryAttempter:(SFErrorRecoveryAttempter)attempter
{
if ([self shouldNotifyUserForError:error] && [self typeForError:error] == SFErrorTypeAlert) {
NSString *title = [self titleForError:error];
NSString *message = [self messageForError:error];
SFAlertMessageView *alert = [[SFAlertMessageView alloc] initWithTitle:title
message:message
image:nil
cancelButton:@"Cancel"];

SFErrorRecoveryOption option = [self recoveryOptionForError:error];
if (option != SFErrorRecoveryOptionUndefiend) {
NSString *optionTitle = [self titleForRecoveryOptionOfError:error];
SFModalMessageAction optionAction = nil;
if (attempter) {
optionAction = attempter(option);

}
if (optionTitle.notEmpty && optionAction) {
[alert addActionButtonWithTitle:optionTitle action:optionAction];
} else {
#ifdef DEBUG
NSAssert(false, @"Recovery option %ld isn't handled for error %@", option, error.localizedDescription);
#endif
DDLogError(@"Recovery option %ld isn't handled for error %@", option, error.localizedDescription);
}
}
return alert;
}
return nil;
}


This method defines what button will be added to an SFAlertMessageView and what action should be taken.


The algorithm for writing a test unit while using Kiwi would be following:



// create necessary domain, code or userInfo for our error.
ErrorCode someCode = something; // those declared as "typedef NS_ENUM"
NSDictionary *someUserInfoDictionary = @ {
someKeyHere: someObjectHere
};

// create an error object.
NSError *error = [NSError errorWithDomain:nil code:someCode userInfo:someUserInfoDictionary]; NSString

// create an attempter
???

// create necessary title, message, image for our alert:
NSString *title = title;
NSString *message = message;

// create an alert (SFAlertMessageView) that we know we will get
SFAlertMessageView *alert = [[SFAlertMessageView alloc] initWithTittle:title message:message image:nil cancelButton:@"Cancel"];

// create an alert and give it the result of [errorUtility modalMessageWithError: error recoveryAttempter:attempter];
SFAlertMessageView *trueAlert = [errorUtility modalMessageWithError:error recoveryAttempter:attempter];

// equate both alerts.
[[alert should] equal:trueAlert]];


SFErrorRecoveryAttempter (for attempter) is declared as following:



typedef SFModalMessageAction (^ SFErrorRecoveryAttempter)(SFErrorRecoveryOption option);


and SFModalMessageAction :



typedef void (^ SFModalMessageAction)();

@protocol ISFModalMessageView <NSObject>

- (instancetype)initWithTitle:(NSString *)title
message:(NSString *)message
image:(UIImage *)image
cancelButton:(NSString *)cancelTitle;
- (void)addActionButtonWithTitle:(NSString *)title action:(SFModalMessageAction)action;
- (void)show;
- (void)hide:(BOOL)animated completion:(void (^)(void))completion;

@end


SFErrorRecoveryOption:



typedef NS_ENUM(NSUInteger, SFErrorRecoveryOption) {
SFErrorRecoveryOptionUndefiend = 0,

SFErrorRecoveryOptionTryAgain,
SFErrorRecoveryOptionReport,
SFErrorRecoveryOptionSignIn,
SFErrorRecoveryOptionSignUp,
SFErrorRecoveryOptionReset,
SFErrorRecoveryOptionSignInFacebook,
};


If you need more information, just let me know.


Aucun commentaire:

Enregistrer un commentaire