Last active
September 20, 2023 15:49
-
-
Save easylive1989/0d8215b8c3ef81c0b2a11412b34a7e9a to your computer and use it in GitHub Desktop.
2023鐵人賽_D6_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 'product_repository.dart'; | |
import 'product.dart'; | |
import 'wallet.dart'; | |
import 'money_not_enough_exception.dart'; | |
class PurchaseProductService { | |
final ProductRepository productRepository; | |
PurchaseProductService(this.productRepository); | |
void execute(Product product, Wallet wallet) { | |
if (product.price > wallet.money) { | |
throw MoneyNotEnoughException(); | |
} | |
productRepository.purchase(product); | |
} | |
} |
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 'product.dart'; | |
import 'wallet.dart'; | |
import 'product_repository.dart'; | |
import 'purchase_product_service.dart'; | |
import 'purchase_product_service_test.mocks.dart'; | |
@GenerateNiceMocks([MockSpec<ProductRepository>()]) | |
main() { | |
test("purchase product success", () { | |
var mockProductRepository = MockProductRepository(); | |
var purchaseProductService = PurchaseProductService(mockProductRepository); | |
purchaseProductService.execute(const Product(100), Wallet(200)); | |
verify(mockProductRepository.purchase(const Product(100))).called(1); | |
}); | |
} |
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment