Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit: expects method meaning

When I create a new mock I need to call the expects method. What exactly it does? What about its arguments?

$todoListMock = $this->getMock('\Model\Todo_List');         $todoListMock->expects($this->any())             ->method('getItems')             ->will($this->returnValue(array($itemMock))); 

I can't find the reason anywhere (I've tried docs). I've read the sources but I can't understand it.

like image 815
thom Avatar asked Sep 15 '11 14:09

thom


People also ask

What is mock PHPUnit?

Likewise, PHPUnit mock object is a simulated object that performs the behavior of a part of the application that is required in the unit test. The developers control the mock object by defining the pre-computed results on the actions.

Should be called exactly 1 times but called 0 times mockery?

Mockery is by default a stubbing library, not a mocking one (which is confusing because of its name). That means that ->shouldReceive(...) by default is "zero or more times". When using ->once(), you say it should be called zero or one time, but not more. This means it'll always pass.

Why do we use PHPUnit?

PHPUnit is used for unit testing your PHP projects. The word Unit refers to a block of code, method or an individual or independent class. Unit testing is a software testing process in which code blocks are checked to see whether the produced result matches the expectations.


2 Answers

expects() - Sets how many times you expect a method to be called:

$mock = $this->getMock('nameOfTheClass', array('firstMethod','secondMethod','thirdMethod')); $mock->expects($this->once())      ->method('firstMethod')      ->will($this->returnValue('value')); $mock->expects($this->once())      ->method('secondMethod')      ->will($this->returnValue('value')); $mock->expects($this->once())      ->method('thirdMethod')      ->will($this->returnValue('value')); 

If you know, that method is called once use $this->once() in expects(), otherwise use $this->any()

see:
PHPUnit mock with multiple expects() calls

https://phpunit.de/manual/current/en/test-doubles.html#test-doubles.stubs

http://www.slideshare.net/mjlivelyjr/advanced-phpunit-testing

like image 62
Marek Sebera Avatar answered Oct 17 '22 02:10

Marek Sebera


A look into the source code will tell you:

/**  * Registers a new expectation in the mock object and returns the match  * object which can be infused with further details.  *  * @param  PHPUnit_Framework_MockObject_Matcher_Invocation $matcher  * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker  */ public function expects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher); 

And the PHPUnit Manual lists the available Matchers at

  • https://phpunit.de/manual/current/en/test-doubles.html#test-doubles.mock-objects.tables.matchers
  • any() returns a matcher that matches when the method it is evaluated for is executed zero or more times.
  • never() returns a matcher that matches when the method it is evaluated for is never executed.
  • atLeastOnce() returns a matcher that matches when the method it is evaluated for is executed at least once.
  • once() returns a matcher that matches when the method it is evaluated for is executed exactly once.
  • exactly(int $count) returns a matcher that matches when the method it is evaluated for is executed exactly $count times.
  • at(int $index) returns a matcher that matches when the method it is evaluated for is invoked at the given $index.
like image 37
Gordon Avatar answered Oct 17 '22 02:10

Gordon