CakePHP 中的单元测试助手
我创建了一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设置助手时,您必须将表单助手设置为 advHtml 助手的属性:
You have to set the form helper as a property of the advHtml helper when setting up the helpers: