type' null'不是Type' Future< bool的子类型。

发布于 2025-02-01 23:59:25 字数 3100 浏览 3 评论 0原文

当我尝试在我的Flutter Project中实现BLOC测试时,我会遇到以下

type 'Null' is not a subtype of type 'Future<bool>'
package:mynovatium/features/signup/repositories/signup_repository.dart 10:16  MockRepository.createAccountsignup

错误 Inbip_bloc_test.dart

class MockRepository extends Mock implements SignUpRepository {}

void main() async {
  await configureInjection(inj.Environment.test);
  group('SignupBloc', () {
    late SignUpBloc signUpBloc;
    late SignUpRepository signupRepositoryMock;
    setUp(() {
      signupRepositoryMock = MockRepository();
      signUpBloc = SignUpBloc(signUpRepository: signupRepositoryMock);
    });

    test('initial state of the bloc is [AuthenticationInitial]', () {
      expect(SignUpBloc(signUpRepository: signupRepositoryMock).state,
          SignupInitial(),);
    });

    group('SignUpCreateAccount', () {
      blocTest<SignUpBloc, SignUpState>(
        'emits [SignUpCreateAccountLoading, SignupInitial] '
        'state when successfully Signed up',
        setUp: () {
          when(signupRepositoryMock.createAccount(
                'Nevil',
                'abcd',
                '[email protected]',
                'english',
              ),).thenAnswer((_) async  => Future<bool>.value(true));
        },
        build: () => SignUpBloc(signUpRepository: signupRepositoryMock),
        act: (SignUpBloc bloc) => bloc.add(
          const SignUpCreateAccount(
            'Nevil',
            'abcd',
            '[email protected]',
            'english',
          ),
        ),
        expect: () => [
          SignUpCreateAccountLoading(),
          SignupInitial(),
        ],
      );
    });
  });
}

ignup_repository.dart 这是注册存储库的代码。

class SignUpRepository {
  Future<bool> createAccount(String _firstName, String _lastName, String _eMailAddress, String _language) async {
    final Response _response;
    try {
      _response = await CEApiRequest().post(
        Endpoints.createCustomerAPI,
        jsonData: <String, dynamic>{
          'firstName': _firstName,
          'lastName': _lastName,
          'email': _eMailAddress,
          'language': _language,
          'responseUrl': Endpoints.flutterAddress,
        },
      );

      final Map<String, dynamic> _customerMap = jsonDecode(_response.body);
      final CustomerModel _clients = CustomerModel.fromJson(_customerMap['data']);

      if (_clients.id != null) {
        return true;
      } else {
        return false;
      }
    } on KBMException catch (e) {
      final KBMException _exception = e;
      throw _exception;
    }
  }
}

如果有人对这里的问题有任何想法,请帮助!

I'm getting the below error while I'm trying to implement bloc testing in my flutter project

type 'Null' is not a subtype of type 'Future<bool>'
package:mynovatium/features/signup/repositories/signup_repository.dart 10:16  MockRepository.createAccountsignup

Following are the corresponding files that might help identify the cause of the error
signup_bloc_test.dart

class MockRepository extends Mock implements SignUpRepository {}

void main() async {
  await configureInjection(inj.Environment.test);
  group('SignupBloc', () {
    late SignUpBloc signUpBloc;
    late SignUpRepository signupRepositoryMock;
    setUp(() {
      signupRepositoryMock = MockRepository();
      signUpBloc = SignUpBloc(signUpRepository: signupRepositoryMock);
    });

    test('initial state of the bloc is [AuthenticationInitial]', () {
      expect(SignUpBloc(signUpRepository: signupRepositoryMock).state,
          SignupInitial(),);
    });

    group('SignUpCreateAccount', () {
      blocTest<SignUpBloc, SignUpState>(
        'emits [SignUpCreateAccountLoading, SignupInitial] '
        'state when successfully Signed up',
        setUp: () {
          when(signupRepositoryMock.createAccount(
                'Nevil',
                'abcd',
                '[email protected]',
                'english',
              ),).thenAnswer((_) async  => Future<bool>.value(true));
        },
        build: () => SignUpBloc(signUpRepository: signupRepositoryMock),
        act: (SignUpBloc bloc) => bloc.add(
          const SignUpCreateAccount(
            'Nevil',
            'abcd',
            '[email protected]',
            'english',
          ),
        ),
        expect: () => [
          SignUpCreateAccountLoading(),
          SignupInitial(),
        ],
      );
    });
  });
}

signup_repository.dart
This is the code for the signup repository.

class SignUpRepository {
  Future<bool> createAccount(String _firstName, String _lastName, String _eMailAddress, String _language) async {
    final Response _response;
    try {
      _response = await CEApiRequest().post(
        Endpoints.createCustomerAPI,
        jsonData: <String, dynamic>{
          'firstName': _firstName,
          'lastName': _lastName,
          'email': _eMailAddress,
          'language': _language,
          'responseUrl': Endpoints.flutterAddress,
        },
      );

      final Map<String, dynamic> _customerMap = jsonDecode(_response.body);
      final CustomerModel _clients = CustomerModel.fromJson(_customerMap['data']);

      if (_clients.id != null) {
        return true;
      } else {
        return false;
      }
    } on KBMException catch (e) {
      final KBMException _exception = e;
      throw _exception;
    }
  }
}

If anyone has any ideas on what might be the issue here, please help!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假情假意假温柔 2025-02-08 23:59:25

好的,因此在上面的代码中,您还需要将方法固执在模拟存储库中,并覆盖它以使其返回某些Incase null的返回。

class MockRepository extends Mock implements SignUpRepository {
  @override
  Future<bool> createAccount(String? _firstName, String? _lastName, String? _eMailAddress, String? _language) =>
      super.noSuchMethod(Invocation.method(#createAccount, [_firstName, _lastName, _eMailAddress, _language]),
      returnValue:  Future<bool>.value(false),);
}

在上述代码中完成类似的操作效果很好。

Okay so in the above code you need to stub the methods within the mock repository as well and override it to have it return something incase null is being returned.

class MockRepository extends Mock implements SignUpRepository {
  @override
  Future<bool> createAccount(String? _firstName, String? _lastName, String? _eMailAddress, String? _language) =>
      super.noSuchMethod(Invocation.method(#createAccount, [_firstName, _lastName, _eMailAddress, _language]),
      returnValue:  Future<bool>.value(false),);
}

Doing something like that done in the above code works well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文