Methods setUp()
and tearDown()
are invoked before and after each test. But really, is there any real word example about why should I need this?
There is a memory leak in provided example in accepted answer. You should add tearDown:
protected function tearDown()
{
$this->_mockedService = null;
}
PHPUnit creates new test case object for every test method call. So if there are 4 test method's - there are will be 4 objects, and 4 mockedService's will be created. And they wouldn't removed until the end of the script (entire test suite). So you need to delete all objects and unset all variables in tearDown to prevent memory leak.