title: Zend Framework 2 how to test forward in controller using phpunit?
body: <p>How can I test a forward in a controller with PHPUnit?</p>
  
  <p>I have two simple modules (A and B), module A call the module B using a forward.
  here is a simple code that not work :</p>
  
  <p><strong>ModuleA</strong></p>
  
  <pre><code>class ModuleAController extends AbstractRestfulController 
  {              
  protected $em;
  
   public function setEntityManager(EntityManager $em)
      {
          $this-&gt;em = $em;
      }
  
  public function getEntityManager()
  {
      if (null === $this-&gt;em) {
          $this-&gt;em 
          $this-&gt;getServiceLocator()-&gt;get('doctrine.entitymanager.orm_default');
      }
      return $this-&gt;em;
  }
  
   public function getList()
  {  
      $data = array('message' =&gt; 'passed by module A');
  
      $forward = $this-&gt;forward()-&gt;dispatch('ModuleB\Controller\ModuleB');
  
      $data['Message'] = $forward-&gt;getVariable('Message');
      return new JsonModel($data);
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>ModuleB</strong></p>
  
  <pre><code>class ModuleBController extends AbstractRestfulController 
  {
  
   public function setEntityManager(EntityManager $em)
      {
          $this-&gt;em = $em;
      }
  
   public function getEntityManager()
   {
      if (null === $this-&gt;em) {
          $this-&gt;em = $this-&gt;getServiceLocator()-&gt;get('doctrine.entitymanager.orm_default');
      }
  
   }
    public function getList()
   {
      $data = array('Message'=&gt;'passed by module B');
      return new JsonModel($data);
   }
  }
  </code></pre>
  
  <p>And this is a test code :</p>
  
  <pre><code>class ModuleAControllerTest extends AbstractHttpControllerTestCase
  {
      protected $controller;
      protected $request;
      protected $response;
      protected $routeMatch;
      protected $event;
  
      protected function setUp()
      {
          $serviceManager = Bootstrap::getServiceManager();
          $this-&gt;controller = new ModuleAController();
          $this-&gt;request    = new Request();
          $this-&gt;routeMatch = new RouteMatch(array());
          $this-&gt;event      = new MvcEvent();
          $config = $serviceManager-&gt;get('Config');
          $routerConfig = isset($config['router']) ? $config['router'] : array();
          $router = HttpRouter::factory($routerConfig);
  
          $this-&gt;event-&gt;setRouter($router);
          $this-&gt;event-&gt;setRouteMatch($this-&gt;routeMatch);
          $this-&gt;controller-&gt;setEvent($this-&gt;event);
          $this-&gt;controller-&gt;setServiceLocator($serviceManager);
      }
      public function testModuleAControllerCanBeAccessed()
      {        
  
          $result   = $this-&gt;controller-&gt;dispatch($this-&gt;request);
          $response = $this-&gt;controller-&gt;getResponse();
  
          $this-&gt;assertEquals(200, $response-&gt;getStatusCode());
          $this-&gt;assertEquals(200, 1+99+100);
      }
  }
  </code></pre>
  
  <p>And this is the error message :
  There was 1 error:</p>
  
  <pre><code>1) ModuleATest\Controller\ModuleAControllerTest::testModuleAControllerCanBeAccessed
  Zend\ServiceManager\Exception\ServiceNotCreatedException: An exception was raised while creating "forward"; no instance returned
  ....
  Caused by
  Zend\ServiceManager\Exception\ServiceNotCreatedException: Zend\Mvc\Controller\Plugin\Service\ForwardFactory requires that the application service manager has been injected; none found
  ....
  
  FAILURES!
  Tests: 1, Assertions: 0, Errors: 1.
  </code></pre>
  
  <p>Is there any way to make this code work ??Any idea ??</p>
  
  <p>Thank you.</p>