PHPUnit - assertInstanceOf not working

This trips me up again and again, especially working in Symfony.

When you use assertInstanceOf in your Symfony2 tests, don't forget that the namespace must be respected:

<?php

namespace MyProject\ThisBundle\Tests;

class MyMainClassTest 
{
  /**
   * @test
   */
  public function factories()
  {
    $factory = $this->getMyFactory();
    $this->assertInstanceOf('MyFactory',$factory);
  }
}

Sorry, that will fail. You are in namespaces. You have to do use the full namespace of the class you are looking for:

<?php

namespace MyProject\ThisBundle\Tests;

class MyMainClassTest 
{
  /**
   * @test
   */
  public function factories()
  {
    $factory = $this->getMyFactory();
    $this->assertInstanceOf('MyProject\MyBundle\Lib\MyFactory',$factory);
  }
}

No comments:

Post a Comment