I would like to write test-cases for the login screen. I'm writing test cases for login action.
- The username and password should satisfy the minimum length 4.
- It should show alert view if the length is < 4
- Want to write test cases for both cases <4 and >4 lengths.
Here is my code:
- (IBAction)loginAction:(id)sender {
if ([[self.userNameTextField text] length] <=3 ||
[[self.passwordTextField text] length] <=3 ) {
UIAlertController *alert = [UIAlertController
alertControllerWithTitle:@"Error"
message:@"Username/Password \n length must be > 4 charecters"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
[alert dismissViewControllerAnimated:true completion:nil];
}];
[alert addAction:action];
[self presentViewController:alert animated:true completion:nil];
} else {
// success case
}
}
Cedar Spec
describe(@"LoginViewController", ^{
__block LoginViewController *subject;
context(@"it should show the alert",^{
beforeEach(^{
subject = [LoginViewController instanceFromStoryboardForSpecs:subject identifier:@"login"];
UITextField *txtUserName = [UITextField new];
subject.userNameTextField = txtUserName;
subject.userNameTextField.text = @"DA";
[subject loginAction: nil];
});
it(@"it should be charecters < 3 ", ^{
subject.userNameTextField.text.length should be_lte(3);
});
it(@"when be charecters < 3 ", ^{
subject.presentedViewController should be_instance_of([UIAlertController class]);
UIAlertController *alertController = (id)subject.presentedViewController;
alertController.title should equal(@"Error"); // Important for proper styling
alertController.message should equal(@"Username/Password \n length must be > 4 charecters");
alertController.actions.count should equal(1);
alertController.preferredStyle should equal(UIAlertControllerStyleAlert);
UIAlertAction *cancelAction = alertController.actions.firstObject;
cancelAction.title should equal(@"OK");
});
});
});
But its getting failed here subject.presentedViewController should be_instance_of([UIAlertController class]);
Can anyone help me to understand writing test cases? I went through with the Cedar WiKi, but I'm not able to understand how to write test cases for my case.