3

我想为登录屏幕编写测试用例。我正在为登录操作编写测试用例。

  • 用户名和密码应满足最小长度 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 应该是_instance_of([UIAlertController class]);

谁能帮我理解编写测试用例?我使用了Cedar WiKi,但我无法理解如何为我的案例编写测试用例。

4

1 回答 1

0

第一个问题是,instanceFromStoryboardForSpecs做什么?它是否将您的视图控制器添加到 UIWindow?如果没有,任何调用都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 测试用户场景作为一个整体。

于 2018-06-26T20:17:06.980 回答