A test is only worth something if it fails when the code is wrong. A test can pass on the right code and still let a bug through. That happens when the right code and the broken code give the same answer to its question.
Off-by-one bugs work like that. Writing < where you meant <= changes the answer for one value only, the one right on the edge. Say a club lets in people aged 16 or more. A test with age 30 can't tell age >= 16 from age > 16. Tests right on the edge and just past it can:
Test("age on the edge", async () => {
Assert.Equal(true, CanJoin(16));
});
Test("age just below", async () => {
Assert.Equal(false, CanJoin(15));
});
In this lesson, /products?max_price=N lists the products that cost N or less. The Mug costs 12, the Notebook 7, the Lamp 29 and the Pen 2. The "max price" test asks for 10 and expects 2 products. The check runs your tests on the shop and on two broken copies of it. When you're done, Run prints:
Your tests on the shop:
max price ... ok
price equal to max ... ok
price just over max ... ok
3 tests: OK
Your tests on a copy that uses < instead of <=:
max price ... ok
price equal to max ... FAIL
Assert.Equal() Failure: Expected 3, Actual 2
price just over max ... ok
3 tests: 1 failed, so they caught this bug
Your tests on a copy that also lets in products that cost 1 more than max_price:
max price ... ok
price equal to max ... ok
price just over max ... FAIL
Assert.Equal() Failure: Expected 2, Actual 3
3 tests: 1 failed, so they caught this bug
Your task: below "max price", add a test named "price equal to max", which checks that /products?max_price=12 lists 3 products, and a test named "price just over max", which checks that /products?max_price=11 lists 2.
Assignment
Follow the Coding Tutorial to write two tests on the edge of the Mug's price, one for each broken copy.
Hint
The Mug costs exactly 12. With max_price=12 it must be in the list, and with max_price=11 it must be out. Copy the shape of the "max price" test and change the name, the number in the URL and the expected count in Assert.Equal.
Free plan: 30 tutor messages. PRO is unlimited.
