How to test an ajax call using phpunit test case ?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Depending on how your ajax response is generated, this may be easy or more difficult. If you use a controller to generate the response, you can simply call the appropriate function in the unit test and check the returned HTML/XML. For example, if responses are generated by calling functions on a controller object like this:
then you can unit test the response like this:
If your response isn’t generated with a function call like that – if your AJAX request is for an actual page with some dynamic content in it, seems like you could do something like this:
PHPUnit also provides the assertTag assertion for testing that generated HTML and XML contains the tags you expect – it can be a little finicky but it’s more robust than just echoing out the response and comparing to a string.
Ultimately, all you want to know is that, given a certain input (the request parameters), you get the output you expect (the returned HTML or XML). The fact that it’s an AJAX call doesn’t fundamentally alter the equation. If you wanted to test that the AJAX requests are being MADE appropriately, you’d want to do that on the client side with JS testing.