A
A
asdasdqwe2022-01-20 19:27:46
PHP
asdasdqwe, 2022-01-20 19:27:46

How is mock different from stub in phunit?

Mock - used to check the state of an object, for example, to check if a
Stub method was called - used to return false (test) data

Question: Is the following code a mock or a stub? We check the method call, but at the same time return false data

$observer = $this->createMock(Observer::class);

        $observer->expects($this->once())
        ->method('abstractMethod')
        ->will($this->returnValue('wow'));

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nokimaro, 2022-01-20
@asdasdqwe

https://vc.ru/flood/44925-za-chto-ya-cenyu-testiro...

The main difference between stubs and mocks is that in one case we are managing the state and in the other we are managing the behavior.
When we use mocks, we are replacing the entire module with a mock (false, test object that mimics the real one). And stub is a function that always outputs the same result, no matter what was input. Mocks are used to check if the function was called with the correct arguments, and stubs are used to test how the function works with the received response. Stubs are used to check the state of a method, and mocks are used to adjust behavior.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question