title: phpunit ajax call test case using zend
body: <p>How we can test a ajax call using phpunit zend. </p>
  
  <p>This is my ajax call in controller</p>
  
  <pre><code>public function indexAction()
  {   
      if ( $this-&gt;getRequest()-&gt;isPost() )
      {      
  
          if (self::isAjax()) {
  
              $name = $this-&gt;getRequest()-&gt;getParam('name');
              $email = $this-&gt;getRequest()-&gt;getParam('email');            
  
  
               $u = new admin_Model_User();
               $u-&gt;email = $email;
               $u-&gt;name = $name;             
               $u-&gt;save();
  
              if(!empty($u-&gt;id)) $msg = "Record Successfully Added";
              else $msg = "Records could not be added";
  
  
              $this-&gt;results[] = array('status'=&gt;true, 'msg'=&gt;$msg);
  
  
              echo $this-&gt;_helper-&gt;json($this-&gt;results);
          } else {
          echo "none-Ajax Request";
      }
      }else {
  
      echo "Request not post";
  }
  }
  
  private function isAjax() {
      return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) &amp;&amp; strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
  }
  </code></pre>
  
  <p>here is my test case</p>
  
  <pre><code>    class AjaxjsonControllerTest extends ControllerTestCase
      {
      // testing not post area
      public function testIndexAction() {
          $this-&gt;dispatch('/ajaxjson');
          $this-&gt;assertModule('admin');
          $this-&gt;assertController('Ajaxjson');
          $this-&gt;assertAction('index');
      }
  // testing not ajax request
  public function testIndexNonajaxAction() {
          $request = $this-&gt;getRequest();  
        $request-&gt;setMethod('POST');
        $request-&gt;setPost(array(
          'name' =&gt; 'name bar',
          'email' =&gt; 'email x',
        ));
  
          $this-&gt;dispatch('/ajaxjson');
          $this-&gt;assertModule('admin');
          $this-&gt;assertController('Ajaxjson');
          $this-&gt;assertAction('index');
      }
  
          public function testPostAction() {
                $request = $this-&gt;getRequest();
                //$request-&gt;setHeader('X_REQUESTED_WITH','XMLHttpRequest');
                $request-&gt;setHeader('HTTP_X_REQUESTED_WITH','XMLHttpRequest');
  
                $request-&gt;setMethod('POST');
                $request-&gt;setPost(array(
                  'name' =&gt; 'name bar',
                  'email' =&gt; 'email x',
                ));
  
                $this-&gt;dispatch('/ajaxjson');
              }
  
      }
  </code></pre>
  
  <p>but this is not working. Has anyone an idea?</p>