CS FundamentalsHardProduct-company placement

A unit test calls a service that reads the current time with datetime.now() and fails every day after 6 pm. What is the cleanest fix?

  1. A. Run the tests only in the morning
  2. B. Inject a clock (or a time-provider function) into the service, so tests can pass a fixed time
  3. C. Add a sleep until 9 am in the test
  4. D. Mark the test as flaky and ignore it

Correct answer: B. Inject a clock (or a time-provider function) into the service, so tests can pass a fixed time

Hidden dependencies on time, randomness or the network make tests non-deterministic. Passing them in as dependencies lets tests control them - the same idea as dependency injection.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic