Hi everybody,
I'm implementing a test for a controller, and I'm experiencing a
strange problem: it seems that if I call ``testAction`` multiple time
inside the same test function, assertions start to fail as if requests
were not properly sent to controllers.
The example below shows a successful test; as you can notice I created
multiple test functions for the same view, and in each I call
``testAction`` exactly once.
class UsersControllerTest extends ControllerTestCase {
public $fixtures = array('app.user');
/**
* Helper function which automatically converts the output of an action
to
* a JSON object.
*/
public function getJsonResult($url, $args = array()) {
$data = $this->testAction($url, $args);
return json_decode($data, true);
}
public function testJsonView() {
// Empty id
$result = $this->getJsonResult('/users.json', array(
'data' => array(),
'method' => 'GET',
'return' => 'contents'
));
$this->assertType('array', $result);
$this->assertArrayHasKey('error', $result);
$this->assertRegExp('/[rR]equired.*id/', $result['error']);
}
public function testJsonView1() {
// Invalid id
$result = $this->getJsonResult('/users.json', array(
'data' => array('id' => 'a'),
'method' => 'GET',
'return' => 'contents'
));
$this->assertNull($result);
}
public function testJsonView2() {
// Ok
$result = $this->getJsonResult('/users.json', array(
'data' => array('id' => 1),
'method' => 'GET',
'return' => 'contents'
));
$this->assertType('array', $result);
$this->assertArrayHasKey('id', $result);
$this->assertEquals(1, $result['id']);
}
}
Now, if move all those tests inside the same function (like presented
below), I get assertion errors after the third call ``testAction``.
class UsersControllerTest extends ControllerTestCase {
public $fixtures = array('app.user');
/**
* Helper function which automatically converts the output of an action
to
* a JSON object.
*/
public function getJsonResult($url, $args = array()) {
$data = $this->testAction($url, $args);
return json_decode($data, true);
}
public function testJsonView() {
// Empty id
$result = $this->getJsonResult('/users.json', array(
'data' => array(),
'method' => 'GET',
'return' => 'contents'
));
$this->assertType('array', $result);
$this->assertArrayHasKey('error', $result);
$this->assertRegExp('/[rR]equired.*id/', $result['error']);
// Invalid id
$result = $this->getJsonResult('/users.json', array(
'data' => array('id' => 'a'),
'method' => 'GET',
'return' => 'contents'
));
$this->assertNull($result);
// Ok
$result = $this->getJsonResult('/users.json', array(
'data' => array('id' => 1),
'method' => 'GET',
'return' => 'contents'
));
$this->assertType('array', $result);
$this->assertArrayHasKey('id', $result);
$this->assertEquals(1, $result['id']);
}
}
Are there any constrains in calling ``testAction`` inside a test
method? It seems that after the first call , the function starts to
return array().
Regards,
Matteo
--
http://www.matteolandi.net/
--
Our newest site for the community: CakePHP Video Tutorials
http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others
with their CakePHP related questions.
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php