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:
void testAgeOnTheEdge() {
assertEquals(true, canJoin(16));
}
void testAgeJustBelow() {
assertEquals(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. testMaxPrice 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:
testMaxPrice ... ok
testPriceEqualToMax ... ok
testPriceJustOverMax ... ok
3 tests: OK
Your tests on a copy that uses < instead of <=:
testMaxPrice ... ok
testPriceEqualToMax ... FAIL
AssertionError: expected: <3> but was: <2>
testPriceJustOverMax ... 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:
testMaxPrice ... ok
testPriceEqualToMax ... ok
testPriceJustOverMax ... FAIL
AssertionError: expected: <2> but was: <3>
3 tests: 1 failed, so they caught this bug
Your task: below testMaxPrice, add testPriceEqualToMax, which checks that /products?max_price=12 lists 3 products, and testPriceJustOverMax, 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 testMaxPrice and change the name, the number in the URL and the expected count, which goes first in assertEquals.
Free plan: 30 tutor messages. PRO is unlimited.
