【问题标题】:Yii2 Method is not allowed in testing with Codeception使用 Codeception 进行测试时不允许 Yii2 方法
【发布时间】:2017-03-30 14:37:09
【问题描述】:

我尝试使用 Codeception 测试我的代码,但当我测试注销选项时,我的测试(功能和验收)失败。它们因“不允许使用方法 (405)”而失败,因为它们是通过 GET 方法发送的。 我在 Nav 小部件中开箱即用了此代码:

$menuItems[] = ['label' => 'Logout (' . Yii::$app->user->identity['username'] . ')',
                                'url' => ['/site/logout'],
                                'linkOptions' => ['data-method' => 'post']

当我手动单击链接时,此代码可以正常工作。在这种情况下,它们是通过 POST 方法发送的。 但它在测试时不起作用。 为什么会这样?

【问题讨论】:

    标签: yii2 codeception


    【解决方案1】:

    对于注销,您可以添加如下表单,它将使用POST发送请求

    $menuItems[] = '<li>'
            . Html::beginForm(['/site/logout'], 'post')
            . Html::submitButton(
                    'Logout ('.Yii::$app->user->identity['username'].')', ['class' => 'btn btn-link btnlogout']
            )
            . Html::endForm()
            . '</li>';
    

    【讨论】:

    • 谢谢!有用!所以在这种情况下,我们应该在表单标签中包含这个链接。
    猜你喜欢
    • 2015-03-18
    • 2015-07-21
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多