0

I am running some unit test on my Zend Framework application. What I cant understand is that the following test fails:

public function testCreateFaqItem()
{
    $this->LoginUser();
    $this->dispatch('/faq/admin-faq/create');
    var_dump($this->getResponse());
    $this->assertResponseCode(200);
    $this->assertQueryContentContains('h1', 'Create');
    $this->assertController('admin-faq');
    $this->assertAction('edit');
}

it fails if on assertResponseCode(200), when i remove the assertResponseCode(200), the test passes. Any help will be much appreciated.

-- EDIT --

Response object dump :

object(Zend_Controller_Response_HttpTestCase)#1130 (8) {
  ["_body":protected]=>
  array(1) {
    ["default"]=>
    string(0) ""
  }
  ["_exceptions":protected]=>
  array(0) {
  }
  ["_headers":protected]=>
  array(1) {
    [0]=>
    array(3) {
      ["name"]=>
      string(8) "Location"
      ["value"]=>
      string(13) "/user/profile"
      ["replace"]=>
      bool(true)
    }
  }
  ["_headersRaw":protected]=>
  array(0) {
  }
  ["_httpResponseCode":protected]=>
  int(302)
  ["_isRedirect":protected]=>
  bool(true)
  ["_renderExceptions":protected]=>
  bool(false)
  ["headersSentThrowsException"]=>
  bool(true)
}

Thanks

Stephan Grobler
  • 469
  • 1
  • 5
  • 17

3 Answers3

1

With any of the unit testing functions the last parameter is always a message that will display if the test fails. So for this example, no need to do a var_dump. Instead this is how I test for response code of 200:

$this->assertResponseCode(200, 'The response code is ' . $this->getResponse()->getHttpResponseCode());

That message will only display if the test fails and will give me a clue to whats going on. Makes for much cleaner code :)

Chris O'Connell
  • 363
  • 2
  • 11
0

You can debug this by dumping the response object and looking at both the headers and body.

// Within your test, before the assertions
var_dump($this->getResponse()); 
Mike B
  • 31,886
  • 13
  • 87
  • 111
  • i used $this->getResponse()->getHeaders(), which gave me 302 – Stephan Grobler Dec 16 '10 at 20:37
  • Nothing else in the response object that might indicate why it's redirecting? Take a look at the request object, make sure everything looks right. – Mike B Dec 16 '10 at 20:39
  • i edited it, also changed the test, that other one will always error i realized – Stephan Grobler Dec 16 '10 at 20:49
  • sorry ircmaxell, will do it like that next time :( – Stephan Grobler Dec 16 '10 at 21:36
  • @ircmaxwell Why? The OP asked for any help and the FAQ suggests that the most 'helpful' answers be voted up. The most helpful answer is not necessarily *THE* answer especially when the question makes such an answer a near impossibility. – Mike B Dec 16 '10 at 22:18
0

To fix the problem of logging in the user and still testing for the response code 200, i inserted $this->resetResponse(); after logging in the user.

public function testCreateFaqItem()
    {
        $this->LoginUser();
        $this->resetResponse();
        $this->dispatch('/faq/admin-faq/create');
        $this->assertResponseCode(200);
        $this->assertQueryContentContains('h1', 'Create');
        $this->assertController('admin-faq');
        $this->assertAction('create');
    }
Stephan Grobler
  • 469
  • 1
  • 5
  • 17