Yii Framework Forum: Codeception Test Ajax Post request - Yii Framework Forum

Jump to content

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

Codeception Test Ajax Post request

#1 User is offline   QuPsi 

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

Posted 19 February 2018 - 11:46 PM

Hi,

So I'm trying to implement a test to verify that a submission is deleted. Trying to stick with best practices, I use a simple AJAX script to send a POST request to delete an entry from a table. Once the request is executed, the browser is supposed to redirect to another page. In my browser, it behaves exactly as expected, however, in Codeception, the test fails.

What do I need to do in Codeception so that this test passes correctly?

Here is the function for the deletion

public function actionDelete()
    {
        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 here is the test function in Codeception

public function deleteTodaysResponse(FunctionalTester $I)
    {
        $user = $I->grabFixture('user', 'user1');
        $today = $I->grabFixture('today', '1');
        
        $I->amLoggedInAs($user);
        $I->amOnRoute('today/my-today');
        $I->see($today['response']);
        $I->click('delete');
        
        $I->seeInCurrentUrl('today/new-today');
        $I->dontSeeRecord('frontend\models\Today', array('user_id' => $user['id']));
    }


I've also tried replacing
$I->click('delete');


With
$I->sendAjaxPostRequest('delete');


Still no luck. Fails at "$I->seeInCurrentUrl('today/new-today');" in either case.

What am I doing wrong here?
Complete PHP/Yii n00b, so be nice to me :)
0

#2 User is online   jim. 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 182
  • Joined: 28-June 12
  • Location:Germany

Posted 20 February 2018 - 02:43 AM

Are you running functional or acceptance tests?

Ajax will only work with acceptance tests as far as i know.
0

#3 User is offline   QuPsi 

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

Posted 20 February 2018 - 06:39 PM

Ah, it's a functional test.

In light of that information, how can I go about in getting this test to pass properly?

Any advice would be appreciated.
Complete PHP/Yii n00b, so be nice to me :)
0

#4 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 3,710
  • Joined: 10-October 10
  • Location:Denmark

Posted 20 February 2018 - 06:41 PM

/* moved to testing */
"Less noise - more signal"
0

#5 User is offline   QuPsi 

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

Posted 25 February 2018 - 04:41 PM

Anyone?
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