代码之家  ›  专栏  ›  技术社区  ›  Damodar

LoginViewController编写cedar规范

  •  3
  • Damodar  · 技术社区  · 8 年前

    • 用户名和密码应满足最小长度4。
    • 如果长度为<4.
    • 想要为这两种情况编写测试用例<4及>4个长度。

    这是我的密码:

    - (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
        }
    }
    

    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");
    
    
                });
            });
    
    });
    

    subject.presentedViewController应该是([UIAlertController类])的实例;

    有人能帮我理解编写测试用例吗?我完成了任务 Cedar WiKi

    1 回复  |  直到 8 年前
        1
  •  0
  •   deekay    8 年前

    第一个问题是,你知道什么 instanceFromStoryboardForSpecs presentViewController:animated:completion:

    你实例化 LoginViewController UITextField 来自代码?我希望这是沿着视图控制器加载的。

    我会分担责任:

    // 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
    

    有了它,你可以 (使用简单的XCTestCase)验证逻辑,如果需要UI测试错误表示逻辑(可以重用以表示不同类型的错误),或者使用BDD测试用户场景作为一个整体,就像您尝试的那样。