简体   繁体   中英

LoginViewController writing cedar specs

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.

Fist question is, what does instanceFromStoryboardForSpecs do? Does it add your view controller to the UIWindow? If not, any call to presentViewController:animated:completion: will fail with an error (do you see any error in console?) and no alert will be shown.

Second, I'm confused by some parts of your code:

You instantiate LoginViewController from the storyboard, but need to create UITextField from code? I'd expect this to be loaded along the View Controller.

I'd split responsibilities:

// call validateUserName:password:, then either presentError: if needed
- (IBAction)loginAction:(id)sender

// login logic
- (void)loginWithUserName:(NSString *)userName password:(NSString *)password

// validation logic
- (BOOL)validateUserName:(NSString *)userName password:(NSString *)password

// use it every time you want to display error
- (BOOL)presentError:(NSString *)error

With that, you can Unit test (using simple XCTestCase) validation logic, if needed UI Test error-presentation logic (which can be reused to present different kinds of errors) or with BDD test user scenarios as a whole like you attempted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM