mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 13:58:47 +00:00
Update mockito in form_app (#904)
This commit is contained in:
@@ -9,12 +9,14 @@ import 'package:mockito/mockito.dart';
|
|||||||
|
|
||||||
class MockClient extends Mock implements http.Client {
|
class MockClient extends Mock implements http.Client {
|
||||||
MockClient() {
|
MockClient() {
|
||||||
when(post('https://example.com/signin', body: anyNamed('body')))
|
when(post('https://example.com/signin',
|
||||||
|
body: anyNamed('body'), headers: anyNamed('headers')))
|
||||||
.thenAnswer((answering) {
|
.thenAnswer((answering) {
|
||||||
dynamic body = answering.namedArguments[const Symbol('body')];
|
dynamic body = answering.namedArguments[const Symbol('body')];
|
||||||
|
|
||||||
if (body != null && body is String) {
|
if (body != null && body is String) {
|
||||||
var decodedJson = json.decode(body) as Map<String, String>;
|
var decodedJson = Map<String, dynamic>.from(
|
||||||
|
json.decode(body) as Map<String, dynamic>);
|
||||||
|
|
||||||
if (decodedJson['email'] == 'root' &&
|
if (decodedJson['email'] == 'root' &&
|
||||||
decodedJson['password'] == 'password') {
|
decodedJson['password'] == 'password') {
|
||||||
|
|||||||
@@ -4,8 +4,8 @@
|
|||||||
|
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
import 'package:http/http.dart' as http;
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:http/http.dart' as http;
|
||||||
import 'package:json_annotation/json_annotation.dart';
|
import 'package:json_annotation/json_annotation.dart';
|
||||||
|
|
||||||
part 'sign_in_http.g.dart';
|
part 'sign_in_http.g.dart';
|
||||||
@@ -86,7 +86,7 @@ class _SignInHttpDemoState extends State<SignInHttpDemo> {
|
|||||||
headers: {'content-type': 'application/json'});
|
headers: {'content-type': 'application/json'});
|
||||||
|
|
||||||
if (result.statusCode == 200) {
|
if (result.statusCode == 200) {
|
||||||
_showDialog('Succesfully signed in.');
|
_showDialog('Successfully signed in.');
|
||||||
} else if (result.statusCode == 401) {
|
} else if (result.statusCode == 401) {
|
||||||
_showDialog('Unable to sign in.');
|
_showDialog('Unable to sign in.');
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -2,8 +2,8 @@
|
|||||||
// for details. All rights reserved. Use of this source code is governed by a
|
// for details. All rights reserved. Use of this source code is governed by a
|
||||||
// BSD-style license that can be found in the LICENSE file.
|
// BSD-style license that can be found in the LICENSE file.
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
|
||||||
import 'package:english_words/english_words.dart' as english_words;
|
import 'package:english_words/english_words.dart' as english_words;
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
class FormValidationDemo extends StatefulWidget {
|
class FormValidationDemo extends StatefulWidget {
|
||||||
const FormValidationDemo({Key key}) : super(key: key);
|
const FormValidationDemo({Key key}) : super(key: key);
|
||||||
|
|||||||
@@ -7,14 +7,14 @@ packages:
|
|||||||
name: _fe_analyzer_shared
|
name: _fe_analyzer_shared
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "14.0.0"
|
version: "22.0.0"
|
||||||
analyzer:
|
analyzer:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: analyzer
|
name: analyzer
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.41.2"
|
version: "1.7.2"
|
||||||
args:
|
args:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -28,7 +28,7 @@ packages:
|
|||||||
name: async
|
name: async
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.6.1"
|
version: "2.8.1"
|
||||||
boolean_selector:
|
boolean_selector:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -42,7 +42,7 @@ packages:
|
|||||||
name: build
|
name: build
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.6.2"
|
version: "1.6.3"
|
||||||
build_config:
|
build_config:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -63,7 +63,7 @@ packages:
|
|||||||
name: build_resolvers
|
name: build_resolvers
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.5.3"
|
version: "1.5.4"
|
||||||
build_runner:
|
build_runner:
|
||||||
dependency: "direct dev"
|
dependency: "direct dev"
|
||||||
description:
|
description:
|
||||||
@@ -105,7 +105,7 @@ packages:
|
|||||||
name: charcode
|
name: charcode
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.2.0"
|
version: "1.3.1"
|
||||||
checked_yaml:
|
checked_yaml:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -168,7 +168,7 @@ packages:
|
|||||||
name: dart_style
|
name: dart_style
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.3.12"
|
version: "1.3.14"
|
||||||
english_words:
|
english_words:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
@@ -276,14 +276,14 @@ packages:
|
|||||||
name: json_annotation
|
name: json_annotation
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "3.1.1"
|
version: "4.0.1"
|
||||||
json_serializable:
|
json_serializable:
|
||||||
dependency: "direct dev"
|
dependency: "direct dev"
|
||||||
description:
|
description:
|
||||||
name: json_serializable
|
name: json_serializable
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "3.5.1"
|
version: "4.0.2"
|
||||||
lints:
|
lints:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -311,7 +311,7 @@ packages:
|
|||||||
name: meta
|
name: meta
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.3.0"
|
version: "1.7.0"
|
||||||
mime:
|
mime:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -325,14 +325,14 @@ packages:
|
|||||||
name: mockito
|
name: mockito
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "4.1.1+1"
|
version: "5.0.7"
|
||||||
package_config:
|
package_config:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: package_config
|
name: package_config
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.9.3"
|
version: "2.0.2"
|
||||||
path:
|
path:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -442,7 +442,7 @@ packages:
|
|||||||
name: test_api
|
name: test_api
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.3.0"
|
version: "0.4.2"
|
||||||
timing:
|
timing:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|||||||
@@ -10,17 +10,17 @@ dependencies:
|
|||||||
flutter:
|
flutter:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
cupertino_icons: ^1.0.0
|
cupertino_icons: ^1.0.0
|
||||||
intl: any
|
intl: ^0.17.0
|
||||||
http: ^0.12.0
|
http: ^0.12.0
|
||||||
mockito: ^4.1.1
|
mockito: ^5.0.0
|
||||||
json_annotation: ^3.0.0
|
json_annotation: any
|
||||||
english_words: ^3.1.0
|
english_words: ^3.1.0
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
json_serializable: ^3.0.0
|
json_serializable: ^4.0.0
|
||||||
build_runner: ^1.10.0
|
build_runner: ^1.11.0
|
||||||
flutter_lints: ^1.0.0
|
flutter_lints: ^1.0.0
|
||||||
|
|
||||||
flutter:
|
flutter:
|
||||||
|
|||||||
52
form_app/test/form_app_test.dart
Normal file
52
form_app/test/form_app_test.dart
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
// Copyright 2021, the Flutter project authors. Please see the AUTHORS file
|
||||||
|
// for details. All rights reserved. Use of this source code is governed by a
|
||||||
|
// BSD-style license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:form_app/src/http/mock_client.dart';
|
||||||
|
import 'package:form_app/src/sign_in_http.dart';
|
||||||
|
import 'package:http/http.dart' as http;
|
||||||
|
|
||||||
|
final http.Client httpClient = MockClient();
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
testWidgets('sign in', (tester) async {
|
||||||
|
await _signIn(tester, 'root','password');
|
||||||
|
expect(find.text('Unable to sign in.'), findsNothing);
|
||||||
|
expect(find.text('Successfully signed in.'), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('sign in with bad password', (tester) async {
|
||||||
|
await _signIn(tester, 'admin','pw');
|
||||||
|
expect(find.byType(AlertDialog), findsOneWidget);
|
||||||
|
expect(find.text('Unable to sign in.'), findsOneWidget);
|
||||||
|
expect(find.text('Successfully signed in.'), findsNothing);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _signIn(WidgetTester tester, String email, String password) async {
|
||||||
|
await tester.pumpWidget(MaterialApp(
|
||||||
|
home: SignInHttpDemo(
|
||||||
|
httpClient: httpClient,
|
||||||
|
),
|
||||||
|
));
|
||||||
|
|
||||||
|
var textFormField = find.byType(TextFormField);
|
||||||
|
expect(textFormField, findsNWidgets(2));
|
||||||
|
|
||||||
|
// Enter email / password
|
||||||
|
var emailField = textFormField.at(0);
|
||||||
|
var passwordField = textFormField.at(1);
|
||||||
|
await tester.enterText(emailField, email);
|
||||||
|
await tester.enterText(passwordField, password);
|
||||||
|
|
||||||
|
// Sign in
|
||||||
|
var button = find.byType(TextButton);
|
||||||
|
expect(button, findsOneWidget);
|
||||||
|
await tester.tap(button);
|
||||||
|
|
||||||
|
// Wait for dialog
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.byType(AlertDialog), findsOneWidget);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user