Yii Framework Forum

Yii2 Functional test send ajax get request not working

(GaryC) #1

I am trying to test an ajax request with a basic install of Yii2. I am just using the SiteController::actionAbout() method to try this out.

public function actionAbout()
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

    return [
        'message' => 'hello world',
        'code' => 100,

And in the test:

public function sendAjax(\FunctionalTester $I)
    $r = $I->grabResponse();


The grab response method is one I wrote myself in a \Helper\Functional.php file:

public function grabResponse()
    return $this->getModule('Yii2')->_getResponseContent();

When I dump out the response, I just see the html from the site/index page. This happens for any route except the site default. If I try to do the same thing using site/index then it returns:

string(36) "{"message":"hello world","code":100}"

What am I missing / doing wrong?