Strategic architecture, tactical design, and testable code principles (SOLID, Clean Architecture, Design Patterns, Testable Design)
97
97%
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Passed
No known issues
Given the following repository interface and use case, produce the three standard test doubles: a Stub, a Mock, and a Fake.
// src/ports/IProductRepository.ts
export interface Product {
id: string
name: string
price: number
stock: number
}
export interface IProductRepository {
findById(id: string): Promise<Product | null>
save(product: Product): Promise<void>
findAll(): Promise<Product[]>
}
// src/use-cases/ReserveProductUseCase.ts
export class ReserveProductUseCase {
constructor(private repo: IProductRepository) {}
async execute(productId: string, quantity: number): Promise<void> {
const product = await this.repo.findById(productId)
if (!product) throw new Error('Product not found')
if (product.stock < quantity) throw new Error('Insufficient stock')
product.stock -= quantity
await this.repo.save(product)
}
}Produce a single file test-doubles.ts that contains and exports all three implementations:
findById (a product with id "prod-1", name "Widget", price 9.99, stock 10). save and findAll are no-ops.save was called and with what product. Exposes a savedProduct property (initially null). findById returns a product with stock 5.Map. All three methods must work correctly.All three classes must implement IProductRepository.
clean-architecture
evals
references
design-patterns
solid-principles
testable-design