Как выполнить метод из функции с состоянием в модульном тесте во флаттере? - PullRequest
0 голосов
/ 21 апреля 2020

У меня есть экран авторизации телефона, на котором я проверяю, ввел ли пользователь действительный номер мобильного телефона 10 di git. Если нет, то отображается текст ошибки. Поскольку я новичок в тестах по флаттеру, я не могу понять, как найти текст. Пробовал с использованием find.text, но возвращает:

The following TestFailure object was thrown running a test:
  Expected: 'Please enter a valid 10 digit mobile number'
  Actual: _TextFinder:<exactly one widget with text "Please enter a valid 10 digit mobile number"
(ignoring offstage widgets): Text("Please enter a valid 10 digit mobile number", debugLabel:
((englishLike caption 2014).merge((blackMountainView caption).apply)).copyWith, inherit: false,
color: Color(0xffd32f2f), family: ProductSans, size: 12.0, weight: 400, baseline: alphabetic,
decoration: TextDecoration.none, textAlign: start, overflow: ellipsis, dependencies: [MediaQuery,
DefaultTextStyle])>
   Which: not an <Instance of 'String'>

Код:

class _PhoneLoginState extends State<PhoneLogin> {
  final TextEditingController _phoneNumberController = TextEditingController();

  bool isValid = false;

  validate(String text) {
    if (text.length == 10)
      setState(() {
        isValid = true;
      });
    else
      setState(() {
        isValid = false;
      });
  }

  @override
  Widget build(BuildContext context) {
    final screenHeight = MediaQuery.of(context).size.height;
    final screenWidth = MediaQuery.of(context).size.width;
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        fontFamily: 'ProductSans',
      ),
      home: Scaffold(
        backgroundColor: white.color,
        resizeToAvoidBottomPadding: false,
        body: SingleChildScrollView(
          child: Column(
             Container(
                                child: Text(
                                  'Enter your mobile number',
                                  style: TextStyle(
                                    fontSize: headSize.fontSize,
                                    fontWeight: FontWeight.w400,
                                    color: black.color,
                                    fontFamily: 'ProductSans',
                                  ),
                                ),
                              ),
                              Container(
                                height: screenHeight / 75,
                              ),
                              Container(
                                height: screenHeight / 15,
                                child: Text(
                                  'We will send you SMS verification code',
                                  style: TextStyle(
                                    fontSize: paraSize.fontSize,
                                    fontWeight: FontWeight.w500,
                                    fontFamily: 'ProductSans',
                                    color: grey.color,
                                    wordSpacing: 0,
                                  ),
                                ),
                              ),
                              Container(
                                height: screenHeight / 10,
                                child: TextField(
                                  key: Key('phoneTextField'),
                                  keyboardType: TextInputType.number,
                                  controller: _phoneNumberController,
                                  autofocus: true,
                                  maxLength: 10,
                                  style: TextStyle(
                                    color: black.color,
                                    fontSize: mobBoxSize.fontSize,
                                  ),
                                  decoration: InputDecoration(
                                    errorText: !isValid
                                        ? 'Please enter a valid 10 digit mobile number'
                                        : null,
                                    counterStyle: TextStyle(color: white.color),
                                    filled: true,
                                    fillColor: white.color,
                                    focusedBorder: OutlineInputBorder(
                                      borderRadius: BorderRadius.circular(7),
                                      borderSide: BorderSide(
                                        color: cyan.color,
                                        width: screenWidth / 250,
                                      ),
                                    ),
                                    hintText: 'Mobile Number',
                                    hintStyle: TextStyle(
                                        fontSize: mobBoxSize.fontSize,
                                        color: grey.color,
                                        fontWeight: FontWeight.bold),
                                    prefix: Container(
                                      padding: EdgeInsets.fromLTRB(4, 1, 10, 1),
                                      child: Text(
                                        "+91  |",
                                        style: TextStyle(
                                          color: black.color,
                                          fontWeight: FontWeight.bold,
                                          fontSize: mobBoxSize.fontSize,
                                        ),
                                      ),
                                    ),
                                  ),
                                  autocorrect: false,
                                  maxLengthEnforced: true,
                                  cursorColor: white.color,
                                ),
                              ),
                              Container(
                                height: screenHeight / 15,
                                child: Center(
                                  child: SizedBox(
                                    height: screenHeight / 6,
                                    width: double.infinity,
                                    child: RaisedButton(
                                      key: Key('Continue'),
                                      color: indigo.color,
                                      shape: borderRadius,
                                      child: Text(
                                        "Continue",
                                        style: TextStyle(
                                          color: white.color,
                                          fontSize: buttonTextSize.fontSize,
                                          fontWeight: FontWeight.bold,
                                        ),
                                      ),
                                      onPressed: () {
                                        validate(_phoneNumberController.text);
                                        if (isValid) {
                                          nextScreen(
                                              _phoneNumberController.text);
                                        }
                                      },
                                    ),
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                      );
                    },
      ),
    );
  }

}

Любая помощь в написании теста для этого экрана входа будет очень признательна.

...