CakePHP 简单测试助手
我是单元测试的新手,一直在使用 SimpleTest 创建新的组件、控制器和模型测试。我正在使用 CakePHP 测试套件版本 1.2.0.0。我在为助手创建视图时遇到困难,并且互联网没有给我提供任何帮助。这是帮助程序代码:
class MyHelper extends AppHelper
{
var $helpers = array('Session');
function dostuff()
{
$helpervar = $this->Session->read('Data');
if(empty($helpervar))
{
return;
}
}
}
我的测试代码在这里:
App::import('Helper', 'MyHelper');
class MyHelperTest extends CakeTestCase {
function startTest() {
$this->MyHelper = new MyHelperHelper();
}
function testRender() {
$this->MyHelper->dostuff();
}
function tearDown() {
unset($this->Controller);
ClassRegistry::flush();
}
}
我收到的错误:
Fatal error: Call to a member function read() on a non-object.
我想知道是否需要创建一个模拟视图。我对这一切都很陌生,所以任何信息将不胜感激!谢谢!
I am new to unit testing, and have been creating new component, controller, and model tests using SimpleTest. I am using CakePHP Test Suite version 1.2.0.0. I am having trouble creating a view for a helper, and the internet has yielded me no assistance. Here is the helper code:
class MyHelper extends AppHelper
{
var $helpers = array('Session');
function dostuff()
{
$helpervar = $this->Session->read('Data');
if(empty($helpervar))
{
return;
}
}
}
And my test code is here:
App::import('Helper', 'MyHelper');
class MyHelperTest extends CakeTestCase {
function startTest() {
$this->MyHelper = new MyHelperHelper();
}
function testRender() {
$this->MyHelper->dostuff();
}
function tearDown() {
unset($this->Controller);
ClassRegistry::flush();
}
}
And the error I receive:
Fatal error: Call to a member function read() on a non-object.
I am wondering if I need to create a mock view. I am new to all this so any information would be very much appreciated! Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该错误是由
SessionHelper
引起的,因为它不是自动实例化的。您必须在startTest()
方法中手动实例化它:The error is caused by the
SessionHelper
because it is not automagically instantiated. You have to instantiate it manually in thestartTest()
method:手动创建类的实例后,您必须使用
constructClasses()
加载所有组件。After manually creating an instance your class, you have to use
constructClasses()
to load all your components.