Bug resolved: 
https://github.com/cakephp/cakephp/commit/c5ca10ca6932b76f505fec39f6e78b34dfcc5b9e


Cheers,
Matteo

On Fri, Oct 21, 2011 at 11:38 AM, Matteo Landi <landima...@gmail.com> wrote:
> 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/
>



-- 
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
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to