Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> PHPUnit "zagnieżdżone" mocki
Lion
post
Post #1





Grupa: Zarejestrowani
Postów: 148
Pomógł: 14
Dołączył: 23.02.2013

Ostrzeżenie: (0%)
-----


Sprawdzam sobie działanie mocków w PHPUnit 5.2.3. Mam pewien problem z "zagnieżdżonymi" mockami, kod wygląda tak:

  1.  
  2. interface EntityInterface
  3. {
  4.  
  5. public function setAncestor(EntityInterface $parent);
  6.  
  7. public function getAncestor();
  8.  
  9. public function addDescendant(EntityInterface $descendant);
  10.  
  11. public function getName();
  12.  
  13. }
  14.  
  15. abstract class Entity implements EntityInterface
  16. {
  17.  
  18. protected $name;
  19.  
  20. /** @var EntityInterface Ancestor */
  21. protected $ancestor = null;
  22.  
  23. /** @var EntityInterface Descendants */
  24. protected $descendants = array();
  25.  
  26. public function __construct($name)
  27. {
  28. $this->name = $name;
  29. }
  30.  
  31. public function getAncestor()
  32. {
  33. return $this->ancestor;
  34. }
  35.  
  36. public function setAncestor(EntityInterface $ancestor)
  37. {
  38. $this->ancestor = $ancestor;
  39. }
  40.  
  41. public function addDescendant(EntityInterface $descendant)
  42. {
  43. $descendant->setAncestor($this);
  44. $this->descendants[$descendant->getName()] = $descendant;
  45. }
  46.  
  47. public function getName()
  48. {
  49. return $this->name;
  50. }
  51.  
  52. }
  53.  


Test wygląda tak:

  1.  
  2. class EntityTest extends PHPUnit_Framework_TestCase
  3. {
  4.  
  5. public function testEntityDescendantCanBeSet()
  6. {
  7. $entity = $this->getMockBuilder(Entity::class)
  8. ->setMethods(array('addDescendant'))
  9. ->disableOriginalConstructor()
  10. ->getMock();
  11. $descendant_entity = $this->getMockBuilder(Entity::class)
  12. ->setMethods(array('getName', 'setAncestor'))
  13. ->disableOriginalConstructor()
  14. ->getMock();
  15. $entity->expects($this->once())
  16. ->method('addDescendant');
  17. $descendant_entity->expects($this->once())
  18. ->method('setAncestor');
  19. $descendant_entity->expects($this->once())
  20. ->method('getName')
  21. ->will($this->returnValue('entity_name'));
  22.  
  23. $entity->addDescendant($descendant_entity);
  24. }
  25.  
  26. }
  27.  


Dostaję taki komunikat:

Cytat
1) EntityTest::testEntityDescendantCanBeSet
Expectation failed for method name is equal to <string:setAncestor> when invoked 1 time(s).
Method was expected to be called 1 times, actually called 0 times.


Podobny komunikat jest dla getName jeśli test setAncestor jest zakomentowany. Co jest nie tak i jak to można przetestować?
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 14.09.2025 - 14:23