1
0
Fork 0
Browse Source

Add test

provider
Jonas Franz 6 months ago
parent
commit
d4f4822809
  1. 2
      lib/screens/cart/total_price_text.dart
  2. 19
      test/cart_store_test.dart
  3. 14
      test/product_service_mock.dart
  4. 26
      test/total_price_test.dart

2
lib/screens/cart/total_price_text.dart

@ -14,7 +14,7 @@ class TotalPriceText extends StatelessWidget { @@ -14,7 +14,7 @@ class TotalPriceText extends StatelessWidget {
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Gesamtpreis: $totalPrice',
'Gesamtpreis: $totalPrice',
style: Theme.of(context).textTheme.labelLarge?.copyWith(fontSize: 24),
),
),

19
test/cart_store_test.dart

@ -0,0 +1,19 @@ @@ -0,0 +1,19 @@
import 'dart:async';
import 'package:flutter_test/flutter_test.dart';
import 'package:thesis_shop/models/remote_resource.dart';
import 'package:thesis_shop/store/cart_store.dart';
import 'product_service_mock.dart';
void main() {
test('test cart store', () async {
final cartStore = CartStore();
cartStore.updateProductList(const RemoteResource.finished(demoProducts));
final callbackStream = StreamController();
cartStore.addListener(() => callbackStream.add(true));
cartStore.increaseAmount(demoProducts.first);
expect(cartStore.amountOfProduct(demoProducts.first), 1);
await expectLater(callbackStream.stream, emits(true));
});
}

14
test/product_service_mock.dart

@ -1,15 +1,17 @@ @@ -1,15 +1,17 @@
import 'package:thesis_shop/models/product.dart';
import 'package:thesis_shop/service/product_service.dart';
const demoProducts = [
Product(title: 'Bananen', price: 3),
Product(title: 'Äpfel', price: 2),
Product(title: 'Birnen', price: 2.5),
Product(title: 'Kirschen', price: 1.2),
];
class MockedProductService implements ProductService {
@override
Future<List<Product>> fetchProducts() async {
return const [
Product(title: 'Bananen', price: 3),
Product(title: 'Äpfel', price: 2),
Product(title: 'Birnen', price: 2.5),
Product(title: 'Kirschen', price: 1.2),
];
return demoProducts;
}
@override

26
test/total_price_test.dart

@ -0,0 +1,26 @@ @@ -0,0 +1,26 @@
// ignore_for_file: unnecessary_cast
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:provider/provider.dart';
import 'package:thesis_shop/screens/cart/total_price_text.dart';
import 'package:thesis_shop/store/cart_store.dart';
class CartStoreMock extends CartStore {
@override
double get totalPrice => 10.0;
}
void main() {
testWidgets('test total price', (tester) async {
final widgetTree = MaterialApp(
home: ChangeNotifierProvider.value(
value: CartStoreMock() as CartStore,
child: const TotalPriceText(),
),
);
await tester.pumpWidget(widgetTree);
final correctText = find.text("Gesamtpreis: 10.00€");
expect(correctText, findsOneWidget);
});
}
Loading…
Cancel
Save