Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TEST] NoSuchMethodError: The getter 'uid' was called on null. Receiver: null Tried calling: uid #203

Open
ghost opened this issue May 9, 2021 · 0 comments

Comments

@ghost
Copy link

ghost commented May 9, 2021

I've tried fixing using MockFirebaseAuth.instance by defining get in MockFirebaseAuth + initializing FirebaseApp with TestWidgetsFlutterBinding.

Problem code posted below...

firebase_flutter_repository_test.dart:

import 'dart:async';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_flutter_repository/firebase_user_repository.dart';
import 'package:custom_repository_core/custom_repository_core.dart';
import 'package:mockito/mockito.dart';
import 'package:test/test.dart';

void main() {
  group('FirebaseUserRepository', () {
    test('should log the user in anonymously', () async {
      final auth = MockFirebaseAuth();
      final repository = FirebaseUserRepository(auth);

      when(auth.signInAnonymously())
          .thenAnswer((_) => Future.value(MockUserCredential()));

      final entity = await repository.login();

      expect(entity, TypeMatcher<UserEntity>());
    });
  });
}

class MockFirebaseAuth extends Mock implements FirebaseAuth {}

class MockFirestore extends Mock implements FirebaseFirestore {}

class MockUserCredential extends Mock implements UserCredential {}

firebase_user_repository.dart:

import 'dart:async';

import 'package:firebase_auth/firebase_auth.dart';
import 'package:custom_repository_core/custom_repository_core.dart';

class FirebaseUserRepository implements UserRepository {
  final FirebaseAuth auth;

  const FirebaseUserRepository(this.auth);

  @override
  Future<UserEntity> login() async {
    final firebaseUser = await auth.signInAnonymously();

    return UserEntity(
      id: firebaseUser.user.uid,      /// firebaseUser.user shows up as null
      displayName: firebaseUser.user.displayName,
    );
  }
}

pubspec.yaml

environment:
  sdk: '>=2.0.0 <3.0.0'

dependencies:
  flutter:
    sdk: flutter
  custom_repository_core:
    path: ../custom_repository_core
  firebase_auth: ^1.1.3
  cloud_firestore: ^2.0.0

dev_dependencies:
  test:
  mockito:
  flutter_test:
    sdk: flutter

flutter:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants