From a69f8045edff22f611b59e917e449b1cda9edebb Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Tue, 1 Dec 2020 22:12:53 +0100 Subject: [PATCH] Move some stuff to utils.dart --- lib/models/body_weight/weight_entry.dart | 2 +- test/utils.dart | 11 +++++++++++ test/weight_provider_test.dart | 21 +++++++-------------- 3 files changed, 19 insertions(+), 15 deletions(-) create mode 100644 test/utils.dart diff --git a/lib/models/body_weight/weight_entry.dart b/lib/models/body_weight/weight_entry.dart index c81f7845f..a11d22fba 100644 --- a/lib/models/body_weight/weight_entry.dart +++ b/lib/models/body_weight/weight_entry.dart @@ -16,7 +16,7 @@ class WeightEntry { final DateTime date; WeightEntry({ - @required this.id, + this.id, @required this.weight, @required this.date, }); diff --git a/test/utils.dart b/test/utils.dart new file mode 100644 index 000000000..5e06eea75 --- /dev/null +++ b/test/utils.dart @@ -0,0 +1,11 @@ +import 'package:http/http.dart' as http; +import 'package:mockito/mockito.dart'; +import 'package:wger/providers/auth.dart'; + +// Test Auth provider +final Auth testAuth = Auth() + ..token = 'FooBar' + ..serverUrl = 'https://localhost'; + +// Mocked HTTP client +class MockClient extends Mock implements http.Client {} diff --git a/test/weight_provider_test.dart b/test/weight_provider_test.dart index e029f848a..9ad6f4b0e 100644 --- a/test/weight_provider_test.dart +++ b/test/weight_provider_test.dart @@ -2,16 +2,9 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:http/http.dart' as http; import 'package:mockito/mockito.dart'; import 'package:wger/models/body_weight/weight_entry.dart'; -import 'package:wger/providers/auth.dart'; import 'package:wger/providers/body_weight.dart'; -// Create a MockClient -class MockClient extends Mock implements http.Client {} - -// Test Auth provider -final Auth auth = Auth() - ..token = 'FooBar' - ..serverUrl = 'https://localhost'; +import 'utils.dart'; void main() { group('fetchPost', () { @@ -21,7 +14,7 @@ void main() { // Mock the server response when(client.get( 'https://localhost/api/v2/weightentry/', - headers: {'Authorization': 'Token ${auth.token}'}, + headers: {'Authorization': 'Token ${testAuth.token}'}, )).thenAnswer((_) async => http.Response( '{"results": [{"id": 1, "date": "2021-01-01", "weight": "80.00"}, ' '{"id": 2, "date": "2021-01-10", "weight": "99"},' @@ -29,7 +22,7 @@ void main() { 200)); // Load the entries - BodyWeight provider = BodyWeight(auth, []); + BodyWeight provider = BodyWeight(testAuth, []); await provider.fetchAndSetEntries(client: client); // Check that everything is ok @@ -43,7 +36,7 @@ void main() { // Mock the server response when(client.post('https://localhost/api/v2/weightentry/', headers: { - 'Authorization': 'Token ${auth.token}', + 'Authorization': 'Token ${testAuth.token}', 'Content-Type': 'application/json; charset=UTF-8', }, body: '{"id":null,"weight":"80","date":"2021-01-01T00:00:00.000"}')) @@ -52,7 +45,7 @@ void main() { // POST the data to the server final WeightEntry weightEntry = WeightEntry(date: DateTime(2021, 1, 1), weight: 80); - final BodyWeight provider = BodyWeight(auth, []); + final BodyWeight provider = BodyWeight(testAuth, []); final WeightEntry weightEntryNew = await provider.addEntry(weightEntry, client: client); // Check that the server response is what we expect @@ -67,11 +60,11 @@ void main() { // Mock the server response when(client.delete( 'https://localhost/api/v2/weightentry/4/', - headers: {'Authorization': 'Token ${auth.token}'}, + headers: {'Authorization': 'Token ${testAuth.token}'}, )).thenAnswer((_) async => http.Response('', 200)); // DELETE the data from the server - final BodyWeight provider = BodyWeight(auth, [ + final BodyWeight provider = BodyWeight(testAuth, [ WeightEntry(id: 4, weight: 80, date: DateTime(2021, 1, 1)), WeightEntry(id: 2, weight: 100, date: DateTime(2021, 2, 2)), WeightEntry(id: 5, weight: 60, date: DateTime(2021, 2, 2))