Перейти до основного вмісту
← ⚡ JavaScript & the browser·Модуль C8 · Урок 4
ЗавданняDefine computeTotal(items, discount) returning sum(qty*price)*(1-discount). Write a single test 'cart total with 10% discount' using Arrange/Act/Assert: cart = [{price:100,qty:2},{price:50,qty:1}], discount=0.1, expect 225. Use the runAll harness from C8-03.

Arrange–Act–Assert

100 XP7 min
Теорія

Three sections per test

A good test reads as three blocks:

test("cart total includes discount", () => {
  // Arrange
  const cart = [{ price: 100, qty: 2 }, { price: 50, qty: 1 }];
  const discountPercent = 0.1;

  // Act
  const total = computeTotal(cart, discountPercent);

  // Assert
  assertEqual(total, 225, "discounted total");
});
  • Arrange — set up the inputs and dependencies.
  • Act — call the function under test once.
  • Assert — check the result.

Why this pattern survives

It separates "what state we're testing" from "what we're testing about it". When the test fails, you know which third broke — arrange (setup wrong), act (function changed signature), or assert (expectation wrong).

🔒

Зареєструйся щоб кодувати

Теорія відкрита всім. Інтерактивний редактор, живий перегляд і перевірка відкриваються з 7-денним безкоштовним trial — потрібна картка, скасування у будь-який час.

Реєстрація — безкоштовний trial →

Перші 10 уроків у кожному треку — безкоштовні. Картка не потрібна.

← НазадНаступний урок →

Отримуй одну пораду з Python чи вебу — щодня поштою

Коротко, без води, без спаму. Відписка — в один клік.