Last active
September 21, 2023 15:17
-
-
Save easylive1989/73f6ee540a785ea66f3cebfc442c61b7 to your computer and use it in GitHub Desktop.
2023鐵人賽_D7_2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MoneyNotEnoughException implements Exception {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:equatable/equatable.dart'; | |
class Product extends Equatable { | |
final double price; | |
const Product(this.price); | |
@override | |
List<Object?> get props => [price]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'product.dart'; | |
class ProductRepository { | |
Future<void> purchase(Product product) async {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter_test/flutter_test.dart'; | |
import 'package:mockito/annotations.dart'; | |
import 'package:mockito/mockito.dart'; | |
import 'retry_purchase_product_service.dart'; | |
import 'product.dart'; | |
import 'wallet_repository.dart'; | |
import 'product_repository.dart'; | |
import 'purchase_product_service_retry_test.mocks.dart'; | |
@GenerateNiceMocks([MockSpec<ProductRepository>(), MockSpec<WalletRepository>()]) | |
main() { | |
test("retry when get wallet fail", () { | |
var mockProductRepository = MockProductRepository(); | |
var mockWalletRepository = MockWalletRepository(); | |
when(mockWalletRepository.get()).thenThrow(Exception()); | |
var purchaseProductService = PurchaseProductService(mockProductRepository, mockWalletRepository); | |
expect(() => purchaseProductService.execute(const Product(100)), throwsA(isA<Exception>())); | |
verify(mockWalletRepository.get()).called(2); | |
}); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'product_repository.dart'; | |
import 'wallet_repository.dart'; | |
import 'product.dart'; | |
import 'wallet.dart'; | |
import 'money_not_enough_exception.dart'; | |
class PurchaseProductService { | |
final ProductRepository productRepository; | |
final WalletRepository walletRepository; | |
PurchaseProductService(this.productRepository, this.walletRepository); | |
Future<void> execute(Product product) async { | |
var wallet = await _getWallet(); | |
if (product.price > wallet.money) { | |
throw MoneyNotEnoughException(); | |
} | |
productRepository.purchase(product); | |
} | |
Future<Wallet> _getWallet() async { | |
try { | |
return await walletRepository.get(); | |
} catch (e) { | |
return await walletRepository.get(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Wallet { | |
final double money; | |
Wallet(this.money); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'wallet.dart'; | |
class WalletRepository { | |
Future<Wallet> get() async => Wallet(100); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment