mardi 6 janvier 2015

How to mock a class created inside the tested class?

In the PHPUnit documentation, the section dealing with mocks explain how to attach a mock object inside the tested class :



...
$observer = $this->getMockBuilder('Observer')
->setMethods(array('update'))
->getMock();
...
$subject = new Subject('My subject');
$subject->attach($observer);
$subject->doSomething();


In my case, I want to test a Subject method. This method create many objects like this:



public function myMethod() {
for($i = 0; $i < 10; $i++) {
$this->collection []= new Observer(/* some data */);
}
}


How can I mock all of the Observers?


Aucun commentaire:

Enregistrer un commentaire