CakePHP中的单元测试助手

时间:2010-12-22 10:17:10

标签: unit-testing cakephp simpletest

我创建了一个名为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;
    }
}

我为它创建了一个测试

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'));
    }
}

当我尝试运行测试时出现以下错误。帮助程序代码的第10行是对表单帮助程序的调用。

  

致命错误:在/opt/lampp/htdocs/mali/app/views/helpers/adv_html.php中调用非对象上的成员函数input()

如何测试调用另一个助手的助手?

  

第10行

编辑:已回答。更新了我的最终测试用例以供参考。

1 个答案:

答案 0 :(得分:3)

在设置帮助程序时,您必须将表单助手设置为advHtml帮助程序的属性:

public function startTest() {
    $this->advHtml = new AdvHtmlHelper();
    $this->advHtml->Form = new FormHelper();
}