How to test ajax call using phpunit test cases?

How to check ajax call using phpunit test case?

+3
source share
1 answer

Depending on how your ajax response is generated, this can be easy or harder. If you use a controller to generate a response, you can simply call the corresponding function in the unit test and check the returned HTML / XML. For example, if responses are generated by function calls on a controller object, for example:

$htmlResponse = $controller->buildPage($_REQUEST);

then you can answer unit test as follows:

$expected = "<html>whatever html you expect</html>";
$test_request = array(...); // parameters to test
$actual = $controller->buildPage($test_request);

$this->assertEquals($expected, $actual);

, , - AJAX , , - :

$_POST['parameter1'] = "test value";  // stuff whichever request object you're using
                                      // with the values you need
ob_start();
include('ajax_page.php');
$actual = ob_get_clean();
$expected = "<html>expected html as above</html>";

$this->assertEquals($expected, $actual);

PHPUnit assertTag , HTML XML , , - , , .

, , , , , ( ), ( HTML XML). , AJAX, . , AJAX- , JS.

+4

Source: https://habr.com/ru/post/1795672/


All Articles