CakePHP 中的单元测试助手

发布于 2024-10-08 21:39:37 字数 2076 浏览 0 评论 0原文

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宫墨修音 2024-10-15 21:39:38

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

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

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文