Question

I created a new helper called AdvHtmlHelper.

class AdvHtmlHelper extends AppHelper {

    var $helpers = array('Form');

    function textbox($fieldName, $options = array()) {
        $output = $this->Form->input($fieldName, array('before' => '<div class="outerdiv"><div class="leftfields"><div class="txt1">', 'between' => '</div><div class="colon"> : </div></div><div class="rightfields"><div class="input">'));
        $output .= '</div></div></div><div class="space"></div>';
        return $output;
    }
}

And I created a test for it

App::import('Helper', 'AdvHtml');
App::import('Helper', 'Form');
App::import('Helper', 'Html');
App::import('Core', 'View');

class AdvHtmlTest extends CakeTestCase {
    private $advHtml = null;

    //Here we instantiate our helper, and all other helpers we need.
    public function startTest() {
        $this->advHtml = new AdvHtmlHelper();
        $this->advHtml->Form = new FormHelper();
        $this->advHtml->Form->Html = new HtmlHelper();
        $this->view = new View($this->Controller);
    }

    //testing textbox() function.
    public function testTextbox() {
        $result = '<div class="input text"><div class="outerdiv"><div class="leftfields"><div class="txt1"><label for="new">New</label></div><div class="colon"> : </div></div><div class="rightfields"><div class="input"><input name="data[new]" type="text" id="new" /></div></div></div></div><div class="space"></div>';
        $this->assertEqual($result, $this->advHtml->textbox('new'));
    }
}

I get the following error when I try to run the test. Line 10 of the helper code is the call to the form helper.

Fatal error: Call to a member function input() on a non-object in /opt/lampp/htdocs/mali/app/views/helpers/adv_html.php

How do I test a helper which calls another helper?

on line 10

EDIT: Answered. Updated with my final test case for reference.

Was it helpful?

Solution

You have to set the form helper as a property of the advHtml helper when setting up the helpers:

public function startTest() {
    $this->advHtml = new AdvHtmlHelper();
    $this->advHtml->Form = new FormHelper();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top