Framework: [Request] We need an easier way to test ajax calls.

Created on 20 Aug 2013  Â·  10Comments  Â·  Source: laravel/framework

Right now it's pretty cumbersome to write unit tests for controller methods that are tested with ajax. Some simple helper functions like:

$this->assertAjaxResponse()

and

$this->ajax($method, $uri, $params)

would be awesome.

All 10 comments

I guess I don't totally understand the difference between an AJAX and non-AJAX call as far as the server is concerned. You can test the response results either way.

The $this->ajax() method would probably automatically add the HTTP_X-Requested-With: XMLHttpRequest header. Adding it manually is the only way to get Request::ajax() to return TRUE while testing controller methods.

+1

+1 I've got middleware that restricts calls to AJAX only, and it's rather painful to test.

@taylorotwell The server would probably respond with a JSON or XML when an AJAX call is made, and with HTML etc. when a non ajax call is made.

Sorry! Are there any feature for disable middleware to testing?

In 5.1 there is

On Sunday, May 10, 2015, Mygmarsuren [email protected] wrote:

Are there any feature for disable middleware to testing?

—
Reply to this email directly or view it on GitHub
https://github.com/laravel/framework/issues/2138#issuecomment-100732127.

+1 for this feature. This would really clean up testing code and make it easier to work with.

@selmonal use the WIthoutMiddleware trait in your test suite :)

It would be nice indeed to have an ->ajax helper that automatically adds ['X-Requested-With' => 'XMLHttpRequest'] @taylorotwell

Was this page helpful?
0 / 5 - 0 ratings