0

I am new to the Unit Testing portion of CakePHP. There is no better time to learn than now.

I have ready through all of the documentation and numerous tutorials on the web. I have run up against a snag during the completion of some of my more basic unit tests.

My Controller:

class TestsController extends AppController {

    public $components = array('RequestHandler', 'Security');

    public $helpers = array('Js');

    public $uses = array('Tests');

    public function beforeFilter() {
        $this->layout = 'admin';
        parent::beforeFilter();
    }

/**
 * Returns database values of all tests that have been created.
 *
 */
    public function index() {
        if ($this->request->is('requested')) {
            return $tests;
        }
        $this->paginate = array(
            'fields' => array('id', 'name', 'email', 'access_token', 'access_token_begins',
                'filesizelimit', 'related_files', 'access_token_expires'),
            'limit' => 10,
            'order' => array(
                'created' => 'desc'
        ));
        $this->set('tests', $this->paginate('Test'));
    }

My Controller test looks like this:

    class TestsControllerTest extends ControllerTestCase {

    public $fixtures = array('app.test');

    public function testIndex() {

        $result = $this->testAction('tests/index');
        debug($result);
    }
    }

The output of my test is:

PHPUNIT_FRAMEWORK_ERROR_NOTICE
Undefined variable: tests
Test case: testsControllerTest(testIndex)

The only viable solution to the error I am receiving would be that maybe the test controller needs authentication before it can actually run the testAction() ?

This is a very simple sample from the CakePHP Documentation guide, yet I receive and undefined variable error. Any help is greatly appreciated.

6
  • do you have a variable called $tests being used in your index view? Commented Mar 31, 2014 at 22:37
  • yes, I do have a variable labelled $tests. Any other Ideas? Commented Mar 31, 2014 at 22:48
  • Are you setting it to the view in your tests/index action? Commented Mar 31, 2014 at 22:49
  • Yes, the original controller is setting the variable via: $this->set('tests', $this->paginate('Test')); Commented Mar 31, 2014 at 23:00
  • It should be a valid URL just as you would call it from the frontend - try testAction('/tests') (note the beginning slash). Also, you should post your Controller code - and: You should always mention the exact cakephp version you are using. Commented Mar 31, 2014 at 23:31

1 Answer 1

1

I have managed to answer my own question.

The $tests variable is undefined because of the if statement within the controller:

public function index() {
    if ($this->request->is('requested')) {
        return $tests;
    }
    $this->paginate = array(
        'fields' => array('id', 'name', 'email', 'access_token', 'access_token_begins',
            'filesizelimit', 'related_files', 'access_token_expires'),
        'limit' => 10,
        'order' => array(
            'created' => 'desc'
    ));
    $this->set('tests', $this->paginate('Test'));
}

That statement does not get hit when running the application, but it does when running the test. In this given function there is no need for the IF statement and therefore my error of UNDEFINED VARIABLE is now resolved.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.