Sobes.tech
Junior — Middle

Mis vahe on mock-objektide ja stubide vahel testimise kontekstis?

sobes.tech AI

Vastus AI-lt

In the testing context, mock objects (mocks) and stubs serve as replacements for real components, but have different purposes:

  • Stub — is a simple object that returns predefined data, allowing the test to operate with predictable behavior. It does not verify how exactly it is called, only provides the necessary responses.

  • Mock — is an object that not only imitates behavior but also verifies that methods are called with expected parameters and in the correct order. Mocks are used to verify interactions.

Example:

  • A stub can return a fake response from a network request.
  • A mock will verify that the request method was called exactly once with a specific URL.

In iOS, frameworks like OCMock or Cuckoo are often used to create mocks and stubs.