import 'package:belunotes/services/auth/auth_exceptions.dart';
import 'package:belunotes/services/auth/auth_provider.dart';
import 'package:belunotes/services/auth/auth_user.dart';
import 'package:test/test.dart';
void main() {
group('Mock Authentication', () {
final provider = MockAuthProvider();
test('Should not be initialized to begin with', () {
expect(provider.isInitialized, false);
});
test('Cannot log out if not initialized', () {
expect(
provider.logOut(),
throwsA(const TypeMatcher<NotInitializedException>()),
);
});
test('Should be able to be initialized', () async {
await provider.initialize();
expect(provider.isInitialized, true);
});
test('User should be null after initialization', () {
expect(provider.currentUser, null);
});
test(
'Should be able to initialize in less than 2 seconds',
() async {
await provider.initialize();
expect(provider.isInitialized, true);
},
timeout: const Timeout(Duration(seconds: 2)),
);
test('Create user should delegate to logIn function', () async {
final badEmailUser = provider.createUser(
email: 'foo@bar.com',
password: 'anypassword',
);
expect(badEmailUser,
throwsA(const TypeMatcher<UserNotFoundAuthException>()));
final badPasswordUser = provider.createUser(
email: 'someone@bar.com',
password: 'foobar',
);
expect(badPasswordUser,
throwsA(const TypeMatcher<WrongPasswordFoundAuthException>()));
final user = await provider.createUser(
email: 'foo',
password: 'bar',
);
expect(provider.currentUser, user);
expect(user.isEmailVerified, false);
});
test('Logged in user should be able to get verified', () {
provider.sendVerificationEmail();
final user = provider.currentUser;
expect(user, isNotNull);
expect(user!.isEmailVerified, true);
});
test('Should be able to log out and log in again', () async {
await provider.logOut();
await provider.logIn(
email: 'email',
password: 'password',
);
final user = provider.currentUser;
expect(user, isNotNull);
});
});
}
class NotInitializedException implements Exception {}
class MockAuthProvider implements AuthProvider {
AuthUser? _user;
var _isInitialized = false;
bool get isInitialized => _isInitialized;
@override
Future<AuthUser> createUser({
required String email,
required String password,
}) async {
if (!isInitialized) throw NotInitializedException();
await Future.delayed(const Duration(seconds: 1));
return logIn(
email: email,
password: password,
);
}
@override
AuthUser? get currentUser => _user;
@override
Future<void> initialize() async {
await Future.delayed(const Duration(seconds: 1));
_isInitialized = true;
}
@override
Future<AuthUser> logIn({required String email, required String password}) {
if (!isInitialized) throw NotInitializedException();
if (email == 'ayushraj1268@gmail.com') throw UserNotFoundAuthException();
if (password == 'Ayush1268') throw WrongPasswordFoundAuthException();
const user = AuthUser(isEmailVerified: false);
_user = user;
return Future.value(user);
}
@override
Future<void> logOut() async {
if (!isInitialized) throw NotInitializedException();
if (_user == null) throw UserNotFoundAuthException();
await Future.delayed(const Duration(seconds: 1));
_user = null;
}
@override
Future<void> sendVerificationEmail() async {
if (!isInitialized) throw NotInitializedException();
final user = _user;
if (user == null) throw UserNotFoundAuthException();
const newUser = AuthUser(isEmailVerified: true);
_user = newUser;
}
}
error:
I am trying to run tests in flutter with the cases written in code but its giving expected throwing Instance of User Not Found Auth Exception Actual Instance of Future Auth User Which emitted Instance of Auth User How to solve it? I have tried everything to fix it. but it keeps on failing the test case and i dont knwo what to do