我很难尝试单元测试(phpUnit),这是我在ZF2中的一个模块。我要做的是确定当GET参数传递给控制器时,页面上的某个元素是否存在类名。
它都是在浏览器中工作的,但是在尝试单元测试时,我无法获得要识别的get参数。
这是我的单元测试代码:
<?php
namespace ComponentManager\Controller;
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
class ComponentManagerControllerTest extends AbstractHttpControllerTestCase
{
public function setUp()
{
$this->setApplicationConfig(
include 'config/application.config.php'
);
parent::setUp();
}
public function testAdminComponentCodeCanBeAccessed()
{
$this->dispatch('/ComponentManager/requestComponent/product/details-1/details-1', 'GET', array('admin' => 1));
// I also tried: $this->dispatch('/ComponentManager/requestComponent/product/details-1/details-1?admin=1');
$this->assertResponseStatusCode(200);
$this->assertMatchedRouteName('ComponentManager/path');
$this->assertControllerName('ComponentManager\Controller\ComponentManager');
$this->assertControllerClass('ComponentManagerController');
$this->assertActionName('requestComponent');
$this->assertModuleName('ComponentManager');
// test will fail here
$this->assertQuery('div.config-active-wrapper');
}
}当我删除GET中管理参数存在的检查时,"div.config-active-wrapper"选择器工作得很好,但是当我重新添加它时,GET参数根本不被识别。有什么想法吗?
发布于 2014-06-25 10:07:57
这里的问题是,单元测试是一个 CLI 操作,在CLI中没有填充超全局。简单而愚蠢:P
解决方案不是在这里使用$_GET这样的超级全局,而是通过一些ACL和一个控制器传递这个"admin“参数。
https://stackoverflow.com/questions/24404033
复制相似问题