简体   繁体   中英

Codeception functional test not passing

Ok I have a simple button that triggers an AJAX POST request in order to delete something from the database. I don't want to use a simple HTML link to stay in line with best practices (only use POST to modify the state of app).

In any case, here is the button.

<?= Html::button('delete', ['class' => 'btn btn-primary', 'id' => 'deltoday']) ?>

Here is the js that it triggers.

$(document).ready(function() { 
   $('#deltoday').on('click', function () {
     $.ajax({
       url: 'delete',
       type: 'POST' 
     });
   });
});

Here is the controller action

if (Yii::$app->request->isAjax && Yii::$app->request->isPost) {
        $today = Today::findOne(['user_id' => Yii::$app->user->id]);

        if ($today->delete()) {
            return $this->redirect('new-today');
        }
    }

And finally, here is the test in Codeception

$user = $I->grabFixture('user', 'user1');
    $today = $I->grabFixture('today', '1');

    $I->amLoggedInAs($user);
    $I->amOnRoute('today/my-today');
    $I->see($today['response']);
    $I->click('#deltoday');

    $I->seeInCurrentUrl('today/new-today');
    $I->dontSeeRecord('frontend\models\Today', array('user_id' => $user['id']));

When I perform the action on the actual app manually, it works as expected, however, the test fails in Codeception.

My firs thought was that Codeception didn't like the Ajax request, however, I have another test that uses Yii2's pjax functionality that works just fine. Perhaps it's the redirect? Further, given that the pjax works (and replaces content in the HTML normally), I'd like to avoid using an acceptance test.

I'd really like to get this test working and understand what I'm doing wrong here (as I'll need similar tests in the future). Any and all help would be appreciated.

I do think you need acceptance tests for this.

Functional tests work with the PHPBrowser, but the PHPBrowser does not support javascript according to the table on this page:

https://codeception.com/docs/03-AcceptanceTests#Sample-Scenario

And your AJAX is using Javascript of course :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM