Skip to content

Instantly share code, notes, and snippets.

@vaygeth89
Created April 12, 2022 16:05
Show Gist options
  • Save vaygeth89/708b399fb123839be9d67e3737de83bf to your computer and use it in GitHub Desktop.
Save vaygeth89/708b399fb123839be9d67e3737de83bf to your computer and use it in GitHub Desktop.
import 'package:bloc_test/bloc_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:example/repositories/account/account_repository.dart';
import 'package:example/services/device_secure_storage_service.dart';
import 'package:example/states/cubit/auth/user_authentication_cubit.dart';
import 'package:example/viewmodels/auth/authentication_data.dart';
import 'package:example/viewmodels/auth/sign_in.dart';
import 'package:example/viewmodels/profile/user_profile.dart';
import 'user_authentication_cubit_test.mocks.dart';
class MockAccountRepository extends Mock implements AccountApiRepository {}
class MockDeviceStorageService extends Mock
implements DeviceSecureStorageService {}
@GenerateMocks([MockAccountRepository, MockDeviceStorageService])
void main() {
late MockMockAccountRepository mockAccountRepo;
late MockMockDeviceStorageService mockDeviceStorageService;
late SignIn validCredentials;
late UserAuthenticationCubit userAuthCubit;
late AuthenticationData mockAuthData;
group("Testing [UserAuthenticationCubit]", () {
setUp(() {
mockAccountRepo = MockMockAccountRepository();
mockDeviceStorageService = MockMockDeviceStorageService();
mockAuthData = AuthenticationData(
accessToken: "",
refreshToken: "refreshToken",
user: UserProfile(
email: "",
id: "",
phoneNumber: "",
fullName: "",
),
roles: []);
validCredentials =
SignIn(email: "example@test.fake", password: "123456");
userAuthCubit =
UserAuthenticationCubit(mockAccountRepo, mockDeviceStorageService);
});
test("Test [UserAuthenticationInitial] state", () {
expect(userAuthCubit.state.runtimeType, UserAuthenticationInitial);
});
blocTest(
'Test successful [UserAuthenticated] state',
build: () {
when(mockAccountRepo.signIn(signIn: validCredentials))
.thenAnswer((_) async => mockAuthData);
return userAuthCubit;
},
act: (UserAuthenticationCubit cubit) {
cubit.signInUser(validCredentials);
},
expect: () => [
isA<UserAuthenticating>(),
isA<UserAuthenticated>(),
],
);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment