N
N
nyatmeat152020-11-14 00:26:26
PHP
nyatmeat15, 2020-11-14 00:26:26

PHPUNIT expects not resetting between methods, how to win?

In setUp I create a class mock

protected function setUp(): void
  {
    parent::setUp(); // TODO: Change the autogenerated stub
    $this->importMock = $this->getMockBuilder(WriteoffsImportService::class)
      ->disableOriginalConstructor()->getMock();
    }

After that, in the method I set the check for execution 1 time
$this->importMock->expects(self::once())->method('importWriteoffs');

The test is running, everything is fine
In the next test, I also set
$this->importMock->expects(self::once())->method('importWriteoffs');
But when I try to execute the test, it says to me that the method cannot be executed more than 1 time.
How is the state saved if the mock is initialized in setUp on a new one and how to make sure that for each test the check for the number of executions is different, and not one for the whole class?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shamanov, 2020-11-14
@SilenceOfWinter

reset after execution in the tearDown() method and/or use the globals-backup option

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question