Spysetup -
// checkout.js export async function checkout(cart, cardDetails, userEmail) const total = cart.reduce((sum, item) => sum + item.price, 0); const charge = await paymentProcessor.chargeCard(cardDetails, total); if (charge.success) paymentProcessor.sendReceipt(userEmail, charge.transactionId); return orderStatus: 'complete', txId: charge.transactionId ;
;
@Test public void testSpyOnRealList() // SPYSETUP: Real object wrapped List<String> realList = new ArrayList<>(); List<String> spyList = spy(realList); spysetup
@Test public void testStubbingSpy() List<String> list = new ArrayList<>(); List<String> spyList = spy(list);
// payment.js export const paymentProcessor = chargeCard: async (cardDetails, amount) => // Real API call to Stripe return success: true, transactionId: 'tx_123' ; , sendReceipt: (email, transactionId) => // Real email service return true; // checkout
throw new Error('Payment failed');
);
you transform fragile integration tests into robust, maintainable specifications.
expect(result).toBe(5); // Real logic executed expect(addSpy).toHaveBeenCalledTimes(1); expect(addSpy).toHaveBeenCalledWith(2, 3); // checkout.js export async function checkout(cart