Hello.
I am using cake 1.3.0 and php 5.2.13. I'm trying to test a controller
(code is below) using fixtures and a subclass of CakeTestCase.

When I run the test, I get a SQL error: "Table
'coolata_test.suggestions' doesn't exist". So I went debugging until I
found line 287 in cake_test_case.php which removes the created
database tables after the testAction method is run.

I commented out that line and everything seemed to be working fine
until on my testAdd() method I wanted to make assertions about the
rendered output.
There is a SQL statement added to the rendered output:

Query: CREATE TABLE `suggestions` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `fbid` int(20) NOT NULL,
        `user_name` varchar(255) DEFAULT NULL,
        `song_name` varchar(255) DEFAULT NULL,
        `picture_url` varchar(255) DEFAULT NULL,
        `flavor` varchar(255) DEFAULT NULL,
        `created` datetime DEFAULT NULL,        PRIMARY KEY  (`id`))    ;

This is because the table already exists.

This issue preventing me from making assertions and testing my
rendered output. I could test by making assertions about the variables
passed to the view but since CakeWebTests cannot use fixtures, I'm
trying to do that here.

I believe this is a bug in the test suite but maybe I'm missing
soething. Please help!

The code:

********************************************
********************************************
THE CONTROLLER:
********************************************
********************************************
<?php
class SuggestionsController extends AppController {

        var $name = 'Suggestions';
        var $helpers = array('xml', 'Cache');
        var $layout = 'xml';

        var $cacheAction = array('index' => 600); //cache the index action
for 10 minutes

        // GET /suggestions/
        function index() {
                if(!isset($this->params['url']['fbid']) || !is_int($this-
>params['url']['fbid']))
                        $fbid = 0;
                else
                        $fbid = $this->params['url']['fbid'];

                $recent_suggestions = $this->Suggestion->getRecent($fbid);
                $this->set('recent_suggestions', $recent_suggestions);
        }

        // POST /suggestions/add
        function add() {
                if($this->hasPermissions($this->data)){
                        unset($this->data['id']);
                        App::import('Sanitize');
                        $clean_data = Sanitize::clean($this->data);
                        $new_suggestion = array('Suggestion'=>$clean_data);
                        if(!$this->Suggestion->save($new_suggestion)){
                                $errs = $this->Suggestion->invalidFields();
                                $msgs = array_values($errs);
                                $error = array('code'=>400, 
'message'=>$msgs[0]);
                                $this->set('error',$error);
                        }
                }
        }
}
?>

********************************************
********************************************
THE TEST:
********************************************
********************************************
<?php
/* Suggestions Test cases generated on: 2010-05-19 12:05:35 :
1274291795*/
App::import('Controller', 'Suggestions');

class SuggestionsControllerTestCase extends CakeTestCase {
        var $fixtures = array('app.suggestion', 'app.like', 'app.bad_word');
        var $autoFixtures = true;

        function testIndex() {
                $result = $this->testAction('/suggestions/',array(
                        'return'=>'vars',
                        'fixturize'=>true,
                        'method'=>'get',
                        'data'=>array('fbid'=>232323)
                ));
                $this->AssertFalse(isset($result['error']), 'Should not return
error');
                $this->AssertEqual(count($result['recent_suggestions']), 6, 
'Should
return 6 suggestions');
        }

        function testAdd() {
                //Inserting a new suggestion.
                $result = $this->testAction('/suggestions/add',array(
                        'return'=>'render',
                        'fixturize'=>true,
                        'method'=>'post',
                        'data'=>array(
                                'fbid'=>232323,
                                'user_name'=>'Some Nice Guy',
                                'song_name'=>'Some Nice Song',
                                'picture_url'=>'http://nice.com/picture.jpg',
                                'flavor'=>'sweet'
                        )
                ));
                pr($result);
        }
}
?>


********************************************
********************************************
THE UGLY HACK IN cake_test_case.php:
********************************************
********************************************

/**
 * Callback issued when a controller's action has been invoked through
testAction().
 *
 * @param Controller $controller Controller that has been invoked.
 * @param array $params Additional parameters as sent by testAction().
 * @return void
 * @access public
 */
        function endController(&$controller, $params = array()) {
                if (isset($this->db) && isset($this->_actionFixtures) && !
empty($this->_actionFixtures) && $this->dropTables) {
                        foreach ($this->_actionFixtures as $fixture) {
                                //$fixture->drop($this->db);                    
           //HERE IT IS !!!!
                        }
                }
        }

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
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?hl=en

Reply via email to