开发者

Unit testing helpers in CakePHP

开发者 https://www.devze.com 2023-01-31 14:23 出处:网络
I created a new helper called AdvHtmlHelper. class AdvHtmlHelper extends AppHelper { var $helpers = array(\'Form\');

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 cla开发者_Python百科ss="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.


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();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号