2010-12-22 90 views
-1

我创建了一个名为AdvHtmlHelper的新助手。CakePHP中的单元测试助手

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行是对表单助手的调用。

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

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

on line 10

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

+0

为什么你创建测试用例视图对象?似乎并不需要 – mark 2010-12-23 00:32:19

+0

否则不起作用。给我一个错误。我一直在重构它,直到我得到它的工作。 – Nigel 2010-12-23 02:50:03

回答

3

你必须设置窗体助手为advHtml助手的属性设置的助手时:

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