Test not passing but working perfectly in app

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.