OneWoven

Turn a requirement into a verified change

Translate a rule into observable behaviour, test a boundary and explain whether the change is ready to ship.

Free lesson 1 of 3: Turn a requirement into a test

Turn your phone sideways for a larger picture.

Run a boundary test, fix the rule, and run it again

Verify that the validator counts characters after trimming.

Write the expected results from the rule

“Printer 01” has ten characters and passes. “ Slow PC ” has eleven characters as typed and seven after trimming. It fails. A missing value also fails.

Check the untrimmed implementation

A validator that checks title.length >= 10 incorrectly accepts “ Slow PC ”. The boundary test fails because its result differs from the rule.

Repair and repeat the checks

Check typeof title === "string" before evaluating title.trim().length >= 10. The two string cases now give the expected result, and a missing value is rejected without raising an error.

Lesson notes

Read the rule

A support team needs request titles that tell staff what to look for. Accept a title only if it is text and has at least 10 characters after removing spaces at the beginning and end. Spaces inside the title still count.

Before writing code, decide what should happen at the boundary. "Printer 01" contains exactly 10 characters and should pass. " Slow PC " contains 11 characters as typed, but only seven after trimming, so it should fail. A missing value should also fail.

Write down the expected result

A test needs two things: the input and the result the rule requires. If you simply record what the current code does, a defective function can pass its own tests. Write your expected outcomes from the rule first, then compare them with the code.

function acceptsTitle(title) {
  return typeof title === 'string' && title.trim().length >= 10;
}

The type check runs first. If the input is not a string, JavaScript stops evaluating the right-hand side, so a missing value is rejected without trying to call trim().

Practise before reading the feedback

Change the threshold to eight. Decide whether " New card ", " Slow PC " and a missing value should pass, and write a short reason for each decision. Add a fourth example of your own that passes the old rule as well as the new one.

Your response is for practice and is not saved.

Review worked feedback

The title " New card " passes because it contains eight characters after trimming. " Slow PC " still fails because it contains seven. A missing value is not text and fails without raising an error. "Printer 01" passes both rules because it contains 10 characters. Keep all four tests: one proves the intended change and the others make sure the boundaries still behave as expected.

Practise a decision