开发者

Zend Framework Unit Test fails on assertResponseCode(200)

开发者 https://www.devze.com 2023-01-30 18:59 出处:网络
I am running some unit test on my Zend Framework application. What I cant understand is that the following test fails:

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)
  ["_isRedir开发者_如何学编程ect":protected]=>
  bool(true)
  ["_renderExceptions":protected]=>
  bool(false)
  ["headersSentThrowsException"]=>
  bool(true)
}

Thanks


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 :)


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()); 


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');
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消