ethan.thomason
ethan.thomason

Reputation: 11

CakePHP Simpletest helper

I am new to unit testing, and have been creating new component, controller, and model tests using SimpleTest. I am using CakePHP Test Suite version 1.2.0.0. I am having trouble creating a view for a helper, and the internet has yielded me no assistance. Here is the helper code:

class MyHelper extends AppHelper
{
        var $helpers = array('Session');

        function dostuff()
        {
                $helpervar = $this->Session->read('Data');
                if(empty($helpervar))
                {
                        return;
                }
        }
}

And my test code is here:

App::import('Helper', 'MyHelper');


class MyHelperTest extends CakeTestCase {

        function startTest() {
                $this->MyHelper = new MyHelperHelper();
        }

        function testRender() {
                $this->MyHelper->dostuff();
        }

        function tearDown() {
                unset($this->Controller);
                ClassRegistry::flush();
        }

}

And the error I receive:

Fatal error: Call to a member function read() on a non-object.

I am wondering if I need to create a mock view. I am new to all this so any information would be very much appreciated! Thanks!

Upvotes: 0

Views: 405

Answers (2)

Anil
Anil

Reputation: 21910

After manually creating an instance your class, you have to use constructClasses() to load all your components.

$Class = new ClassController();
$Class->constructClasses();

Upvotes: 0

dhofstet
dhofstet

Reputation: 9964

The error is caused by the SessionHelper because it is not automagically instantiated. You have to instantiate it manually in the startTest() method:

function startTest() {
    $this->MyHelper = new MyHelper();
    $this->MyHelper->Session = new SessionHelper();
}

Upvotes: 1

Related Questions