Add unit tests
This commit is contained in:
@@ -8,11 +8,10 @@ enum LoginProvider {
|
||||
FACEBOOK,
|
||||
}
|
||||
|
||||
abstract class AuthenticationProvider {
|
||||
Future<AuthCredential> handleSignIn();
|
||||
void logout();
|
||||
class AuthenticationProviderFactory {
|
||||
const AuthenticationProviderFactory();
|
||||
|
||||
static AuthenticationProvider createAuthProvider(LoginProvider provider) {
|
||||
AuthenticationProvider createAuthProvider(LoginProvider provider) {
|
||||
switch (provider) {
|
||||
case LoginProvider.GOOGLE:
|
||||
return GoogleAuthenticationProvider();
|
||||
@@ -27,3 +26,8 @@ abstract class AuthenticationProvider {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
abstract class AuthenticationProvider {
|
||||
Future<AuthCredential> handleSignIn();
|
||||
void logout();
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import 'package:shared_preferences/shared_preferences.dart';
|
||||
class LoginState with ChangeNotifier {
|
||||
FirebaseAuth _auth;
|
||||
AuthenticationProvider _authenticationProvider;
|
||||
AuthenticationProviderFactory _authenticationProviderFactory;
|
||||
|
||||
SharedPreferences _prefs;
|
||||
|
||||
@@ -17,9 +18,11 @@ class LoginState with ChangeNotifier {
|
||||
LoginState({
|
||||
@required SharedPreferences preferences,
|
||||
@required FirebaseAuth firebaseAuth,
|
||||
AuthenticationProviderFactory authenticationProviderFactory = const AuthenticationProviderFactory(),
|
||||
}) {
|
||||
_prefs = preferences;
|
||||
_auth = firebaseAuth;
|
||||
_authenticationProviderFactory = authenticationProviderFactory;
|
||||
loginState();
|
||||
}
|
||||
|
||||
@@ -29,9 +32,9 @@ class LoginState with ChangeNotifier {
|
||||
|
||||
FirebaseUser currentUser() => _user;
|
||||
|
||||
void login(LoginProvider loginProvider) async {
|
||||
Future<void> login(LoginProvider loginProvider) async {
|
||||
_authenticationProvider =
|
||||
AuthenticationProvider.createAuthProvider(loginProvider);
|
||||
_authenticationProviderFactory.createAuthProvider(loginProvider);
|
||||
|
||||
_loading = true;
|
||||
notifyListeners();
|
||||
@@ -39,11 +42,11 @@ class LoginState with ChangeNotifier {
|
||||
var authCredentials = await _authenticationProvider.handleSignIn();
|
||||
|
||||
if (authCredentials != null) {
|
||||
final FirebaseUser user = await _auth.signInWithCredential(
|
||||
_user = await _auth.signInWithCredential(
|
||||
authCredentials);
|
||||
|
||||
if (user != null) {
|
||||
print("signed in " + user.displayName);
|
||||
if (_user != null) {
|
||||
print("signed in " + _user.displayName);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+225
-1
@@ -1,6 +1,20 @@
|
||||
# Generated by pub
|
||||
# See https://dart.dev/tools/pub/glossary#lockfile
|
||||
packages:
|
||||
_fe_analyzer_shared:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: _fe_analyzer_shared
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.0.3"
|
||||
analyzer:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: analyzer
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.39.4"
|
||||
archive:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -71,6 +85,13 @@ packages:
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.1.1"
|
||||
coverage:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: coverage
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.13.6"
|
||||
crypto:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -78,6 +99,13 @@ packages:
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.1.3"
|
||||
csslib:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: csslib
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.16.1"
|
||||
cupertino_icons:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
@@ -184,6 +212,13 @@ packages:
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "3.1.0"
|
||||
glob:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: glob
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.2.0"
|
||||
google_sign_in:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
@@ -191,6 +226,34 @@ packages:
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "4.0.1+3"
|
||||
html:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: html
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.14.0+3"
|
||||
http:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: http
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.12.0+4"
|
||||
http_multi_server:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: http_multi_server
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.2.0"
|
||||
http_parser:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: http_parser
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "3.1.3"
|
||||
image:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -212,6 +275,20 @@ packages:
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.16.0"
|
||||
io:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: io
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.3.3"
|
||||
js:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: js
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.6.1+1"
|
||||
local_auth:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
@@ -240,6 +317,62 @@ packages:
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.1.8"
|
||||
mime:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: mime
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.9.6+3"
|
||||
mockito:
|
||||
dependency: "direct dev"
|
||||
description:
|
||||
name: mockito
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "4.1.1"
|
||||
multi_server_socket:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: multi_server_socket
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.0.2"
|
||||
node_interop:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: node_interop
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.0.3"
|
||||
node_io:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: node_io
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.0.1+2"
|
||||
node_preamble:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: node_preamble
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.4.8"
|
||||
package_config:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: package_config
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.1.0"
|
||||
package_resolver:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: package_resolver
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.0.10"
|
||||
path:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -268,6 +401,13 @@ packages:
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.2.0"
|
||||
pool:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: pool
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.4.0"
|
||||
provider:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
@@ -275,6 +415,13 @@ packages:
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "3.0.0+1"
|
||||
pub_semver:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: pub_semver
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.4.3"
|
||||
quiver:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -296,11 +443,53 @@ packages:
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.5.2"
|
||||
shelf:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: shelf
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.7.5"
|
||||
shelf_packages_handler:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: shelf_packages_handler
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.0.4"
|
||||
shelf_static:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: shelf_static
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.2.8"
|
||||
shelf_web_socket:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: shelf_web_socket
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.2.3"
|
||||
sky_engine:
|
||||
dependency: transitive
|
||||
description: flutter
|
||||
source: sdk
|
||||
version: "0.0.99"
|
||||
source_map_stack_trace:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: source_map_stack_trace
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.1.5"
|
||||
source_maps:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: source_maps
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.10.9"
|
||||
source_span:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -336,6 +525,13 @@ packages:
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.1.0"
|
||||
test:
|
||||
dependency: "direct dev"
|
||||
description:
|
||||
name: test
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.9.4"
|
||||
test_api:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -343,6 +539,13 @@ packages:
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.2.11"
|
||||
test_core:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: test_core
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.2.15"
|
||||
typed_data:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -364,6 +567,27 @@ packages:
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.0.8"
|
||||
vm_service:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: vm_service
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.3.1"
|
||||
watcher:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: watcher
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.9.7+13"
|
||||
web_socket_channel:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: web_socket_channel
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.1.0"
|
||||
xml:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -379,5 +603,5 @@ packages:
|
||||
source: hosted
|
||||
version: "2.2.0"
|
||||
sdks:
|
||||
dart: ">=2.4.0 <3.0.0"
|
||||
dart: ">=2.6.0 <3.0.0"
|
||||
flutter: ">=1.10.0 <2.0.0"
|
||||
|
||||
@@ -49,6 +49,8 @@ dependencies:
|
||||
get_it: ^3.1.0
|
||||
|
||||
dev_dependencies:
|
||||
test:
|
||||
mockito:
|
||||
flutter_test:
|
||||
sdk: flutter
|
||||
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:como_gasto/auth_providers/auth_provider.dart';
|
||||
import 'package:como_gasto/states/login_state.dart';
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
class MockSharedPreferences extends Mock implements SharedPreferences {}
|
||||
|
||||
class MockFirebaseAuth extends Mock implements FirebaseAuth {}
|
||||
|
||||
class MockAuthenticationProviderFactory extends Mock
|
||||
implements AuthenticationProviderFactory {}
|
||||
|
||||
class MockAuthenticationProvider extends Mock
|
||||
implements AuthenticationProvider {}
|
||||
|
||||
class MockAuthCredential extends Mock implements AuthCredential {}
|
||||
|
||||
class MockFirebaseUser extends Mock implements FirebaseUser {}
|
||||
|
||||
void main() {
|
||||
group("user is not logged in", () {
|
||||
MockSharedPreferences prefs = MockSharedPreferences();
|
||||
|
||||
when(prefs.containsKey('isLoggedIn')).thenReturn(false);
|
||||
|
||||
LoginState state = LoginState(
|
||||
preferences: prefs,
|
||||
firebaseAuth: MockFirebaseAuth(),
|
||||
);
|
||||
|
||||
test("initial state is logged out", () {
|
||||
expect(state.isLoggedIn(), false);
|
||||
});
|
||||
|
||||
test("no user when not logged in", () {
|
||||
expect(state.currentUser(), isNull);
|
||||
});
|
||||
|
||||
test("no loading", () {
|
||||
expect(state.isLoading(), isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
group("when login", () {
|
||||
MockSharedPreferences prefs = MockSharedPreferences();
|
||||
MockAuthenticationProviderFactory authFactory =
|
||||
MockAuthenticationProviderFactory();
|
||||
MockAuthenticationProvider authenticationProvider =
|
||||
MockAuthenticationProvider();
|
||||
MockFirebaseAuth auth = MockFirebaseAuth();
|
||||
|
||||
when(prefs.containsKey('isLoggedIn')).thenReturn(false);
|
||||
when(authFactory.createAuthProvider(LoginProvider.GOOGLE))
|
||||
.thenReturn(authenticationProvider);
|
||||
when(authenticationProvider.handleSignIn()).thenAnswer((_) async => null);
|
||||
|
||||
LoginState state;
|
||||
|
||||
setUp(() {
|
||||
state = LoginState(
|
||||
preferences: prefs,
|
||||
firebaseAuth: auth,
|
||||
authenticationProviderFactory: authFactory,
|
||||
);
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
state.dispose();
|
||||
});
|
||||
|
||||
|
||||
test("should set isLoading to true", () async {
|
||||
expectEvents(state, [
|
||||
() => expect(state.isLoading(), true),
|
||||
() => expect(state.isLoading(), false),
|
||||
]);
|
||||
|
||||
await state.login(LoginProvider.GOOGLE);
|
||||
});
|
||||
|
||||
test("user is logged in if has credentials", () async {
|
||||
var credentials = MockAuthCredential();
|
||||
var user = MockFirebaseUser();
|
||||
when(user.displayName).thenReturn("Test User");
|
||||
|
||||
when(authenticationProvider.handleSignIn())
|
||||
.thenAnswer((_) async => credentials);
|
||||
when(auth.signInWithCredential(credentials))
|
||||
.thenAnswer((_) async => user);
|
||||
|
||||
expectEvents(state, [
|
||||
ignoreEvent,
|
||||
() {
|
||||
expect(state.currentUser(), isNotNull);
|
||||
expect(state.isLoggedIn(), true);
|
||||
},
|
||||
]);
|
||||
|
||||
await state.login(LoginProvider.GOOGLE);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
void ignoreEvent() => null;
|
||||
|
||||
void expectEvents<T extends ChangeNotifier>(
|
||||
T listenable, List<Function> events) {
|
||||
int numCall = 0;
|
||||
listenable.addListener(() {
|
||||
events[numCall++]();
|
||||
});
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
// This is a basic Flutter widget test.
|
||||
//
|
||||
// To perform an interaction with a widget in your test, use the WidgetTester
|
||||
// utility that Flutter provides. For example, you can send tap and scroll
|
||||
// gestures. You can also use WidgetTester to find child widgets in the widget
|
||||
// tree, read text, and verify that the values of widget properties are correct.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
import 'package:como_gasto/main.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
|
||||
// Build our app and trigger a frame.
|
||||
await tester.pumpWidget(MyApp());
|
||||
|
||||
// Verify that our counter starts at 0.
|
||||
expect(find.text('0'), findsOneWidget);
|
||||
expect(find.text('1'), findsNothing);
|
||||
|
||||
// Tap the '+' icon and trigger a frame.
|
||||
await tester.tap(find.byIcon(Icons.add));
|
||||
await tester.pump();
|
||||
|
||||
// Verify that our counter has incremented.
|
||||
expect(find.text('0'), findsNothing);
|
||||
expect(find.text('1'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user