Skip to content

Instantly share code, notes, and snippets.

@easylive1989
Last active September 20, 2023 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save easylive1989/0d8215b8c3ef81c0b2a11412b34a7e9a to your computer and use it in GitHub Desktop.
Save easylive1989/0d8215b8c3ef81c0b2a11412b34a7e9a to your computer and use it in GitHub Desktop.
2023鐵人賽_D6_2
class MoneyNotEnoughException implements Exception {}
import 'package:equatable/equatable.dart';
class Product extends Equatable {
final double price;
const Product(this.price);
@override
List<Object?> get props => [price];
}
import 'product.dart';
class ProductRepository {
Future<void> purchase(Product product) async {}
}
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);
}
}
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);
});
}
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