Zend框架单元测试
问题描述:
我试图用Zend_Test_PHPUnit编写单元测试我的应用程序,但我总是只得到Zend框架单元测试
1) IndexControllerTest::testValidation
Failed asserting last controller used <"error"> was "test"
我创建了一个测试控制器,但即使在那里,我不能让它开始工作。
任何人都可以帮忙吗?
谢谢!
class TestController extends Zend_Controller_Action
{
public function indexAction()
{
print 'test';
}
}
引导是:
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
require_once '的Zend /装载机/ Autoloader.php'; Zend_Loader_Autoloader :: getInstance();
phpunit.xml是:
<phpunit bootstrap="./bootstrap.php">
<testsuite name="Application Test Suite">
<directory>./application</directory>
</testsuite>
<testsuite name="Library Test Suite">
<directory>./library</directory>
</testsuite>
<filter>
<whitelist>
<directory suffix=".php">../library/</directory>
<directory suffix=".php">../application/</directory>
</whitelist>
</filter>
</phpunit>
测试控制器
class IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
public function setUp()
{
$this->bootstrap = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
parent::setUp();
}
public function testValidation()
{
$this->dispatch('/test/');
$this->assertController("test");
}
}
答
看起来像你的的TestController错误。
您的查看是否可用(/test/index.phtml)?
好的解决方案是检查抛出的异常(将单元测试包装在Try/Catch Block中并打印到错误日志中)。
答
我得到了完全相同的错误,但由于完全不同的问题。
一个更普遍的解决方案可能是添加到测试方法
print_r(strip_tags(
$this->bootstrap
->getBootstrap()
->getResource('layout')
->content
)); die;
这是假设正在使用的Zend_Layout这个......它打印出error.phtml文件的内容,所以至少你可以看到究竟发生了什么。
你应该看到:
An error occurred
Application error
Exception information:
Message: {Your Error Message will appear here}
Stack trace:
{A stack trace will follow...}
随着之后出现的特定错误消息“消息:”再经过一个完整的堆栈跟踪“堆栈跟踪”。
希望这至少有助于调试潜在的问题
这是缺少的观点。我只创建了一个JSON-API,这就是为什么我不需要视图。 谢谢! – thesonix 2012-01-05 10:55:47