John Smith
John Smith

Reputation: 6207

Phpunit, expects a method run two times, but what if it didnt run at all?

Well thats interesting. Lets mock:

$stub = $this->getMock ('Testclass');
$stub->expects($this->exactly(2))->method('runthistwice');

so, this fails:

$stub->runthistwice();

this also fails:

$stub->runthistwice();
$stub->runthistwice();
$stub->runthistwice();

and this passes:

$stub->runthistwice();
$stub->runthistwice();

So far so good. But if I dont invoke $stub->runthistwice(); at all, the test still passes! Its not logical! What Im doing wrong?

Upvotes: 1

Views: 67

Answers (1)

Potherca
Potherca

Reputation: 14640

The code posted is valid and correct. If $stub->runthistwice(); is not called at all then PHPUnit will mark the test as failed (as stated in the comments by zerkms).

Logic dictates the defect is elsewhere in the code.

If you attempt to create a Short, Self Contained, Correct, Example with the exact code that is run, the odds are you will find this defect all by yourself.

If you do, it is perfectly acceptable to post your findings as an answer to your own question.

Upvotes: 1

Related Questions