Sobes.tech
Junior — Middle

What is the significance of fake data in unit testing?

sobes.tech AI

Answer from AI

Fakes in unit testing are objects that implement the interface or behavior of real dependencies but with simplified logic, allowing for isolated testing of the module. Unlike mocks, which verify calls, fakes provide real but simplified functionality.

The value of fakes in testing:

  • They help avoid dependence on external systems (e.g., databases, APIs), speeding up tests.
  • They provide more realistic behavior than simple stubs.
  • They help identify logic errors in the module without external influences.

Example: instead of connecting to a real database, a fake repository can be used that stores data in memory.

class FakeUserRepository implements UserRepositoryInterface {
    private $users = [];

    public function addUser(User $user) {
        $this->users[$user->getId()] = $user;
    }

    public function findUserById($id) {
        return $this->users[$id] ?? null;
    }
}

This fake allows testing user logic without a real database.

What is the significance of fake data in unit… - sobes.tech