【问题标题】:Why Cakephp's Controller second test case is always failed为什么 Cakephp 的 Controller 第二个测试用例总是失败
【发布时间】:2020-02-20 11:22:12
【问题描述】:

我正在为我们使用 cakephp 2.x 版本实现的应用程序实现控制器测试用例。

到目前为止,我有 2 个测试用例,并计划做更多。但是,我在第二个测试用例中堆叠并收到失败消息。如果我注释掉第一个测试用例并为第二个测试用例运行它,我得到了预期的结果。唯一的事情是,如果我有两个测试用例,第二个测试用例总是失败。这是我的代码的代码 sn-p。

我的运行命令是./cake test --stderr app Controller/EventsController,得到的错误信息是Undefined index: HTTP_HOST

我知道一个有点奇怪的情况,注释掉第一个测试用例并且永远不会得到这个错误并成功通过第二个测试用例。感谢您的建议和想法。太感谢了。

App::uses('EventsController', 'Controller');
App::uses('UsersController', 'Controller');

class EventsControllerTest extends ControllerTestCase {
    public function setUp(){
        $this->testAction('/users/login/',[
            'method' => 'POST',
            'data' => [
                'User' => [
                    'username'=> 'bk.ll@llmail.com',
                    'password'=> '123456',
                    'clientType'=>'llWeb'
                ]
            ]
        ]);
        parent::setUp();
    }

    public function testEvents(){
        $this->testAction('/admin/events/');
        $this->assertInternalType('array', $this->vars['organisations']);
        $this->assertInternalType('string', $this->vars['userRole']);
        $this->assertInternalType('array', $this->vars['dutyRosterObj']);
        $this->assertInternalType('string', $this->vars['date']);
        $this->assertInternalType('boolean', $this->vars['isKkCdlc']);
    }

    public function testEventsList(){
        $this->testAction('/admin/events/list/');
        $this->assertInternalType('array', $this->vars['events']);
        $this->assertInternalType('array', $this->vars['organisations']);
        $this->assertInternalType('boolean', $this->vars['demoForSchool']);
        $this->assertInternalType('integer', $this->vars['offset']);
    }
}

【问题讨论】:

    标签: php cakephp testcase phpunit


    【解决方案1】:

    您似乎忘记在phpunit.xml.dist 文件中配置$_SERVER 变量:

    <php>
      <server name="HTTP_HOST" value="example.org"/>
    </php>
    

    更多信息:https://phpunit.readthedocs.io/en/8.3/configuration.html

    【讨论】:

    • 好吧,正如我在问题中提到的,如果我注释掉第一个测试用例,如何成功运行第二个测试用例?如果我错过了HTTP_HOST 值,如果我运行第二个测试用例,我应该能够看到错误消息吗?
    • 只是想知道,我应该在 cakephp 2 的哪里添加 phpunit.xml.dist?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-04
    • 2015-10-11
    相关资源
    最近更新 更多