Codeception Test Ajax Post request


#1

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?


#2

Are you running functional or acceptance tests?

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


#3

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.


(Jacob Moen) #4

[color="#006400"]/* moved to testing */[/color]


#5

Anyone?