Yii Framework Forum: Test not passing but working perfectly in app - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Test not passing but working perfectly in app

#1 User is offline   QuPsi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 72
  • Joined: 28-April 14

Posted 06 March 2018 - 07:30 PM

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.
Complete PHP/Yii n00b, so be nice to me :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users