Answers.Body: <p>Allan - I have tended to try to refactor the AJAX functions such that they call a more testable function &amp; test that - this generally improves the code....</p>
  
  <p>There are a few examples that test the calls directly - see classes like</p>
  
  <p>CRM_Contact_Page_AjaxTest
  CRM_Custom_Page_AJAXTest
  CRM_Financial_Page_AjaxTest
  CRM_Group_Page_AjaxTest</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-26T01:19:35.423Z
Answers.LastActivityDate: 2016-07-26T01:19:35.423Z
Answers.UserDisplayName: Eileen
Answers.Body: <p>I'm a bit out of date on the current status of the civi test framework, but if WebTests are still available I would do this as a WebTest.
  <a href="https://wiki.civicrm.org/confluence/display/CRM/Setting+yourself+up+to+work+with+Selenium+tests" rel="nofollow">https://wiki.civicrm.org/confluence/display/CRM/Setting+yourself+up+to+work+with+Selenium+tests</a></p>
  
  <p>Include a line that waits for some element you are expecting to appear, then test its value using the webtest api.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-25T18:01:02.613Z
Answers.Text: It looks like they do still exist. See e.g. https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/WebTest/Contact/AddTest.php, and it looks like there's a utility function you can use $this->waitForAjaxContent().
Answers.UserDisplayName: Demerit
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-26T01:15:18.447Z
Answers.Text: Webtests run - but they are of limited value as they are too slow & unreliable to run as part of CI :-(
Answers.UserDisplayName: Eileen
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-25T17:57:14.203Z
Answers.LastActivityDate: 2016-07-25T17:57:14.203Z
Answers.UserDisplayName: Demerit
Body: <p>There are several methods in CiviCRM that just print their output directly to the buffer and then exit, because they're just sending an HTML snippet or other response for an AJAX call. For example, CRM_Event_Cart_Page_CheckoutAJAX::add_participant_to_cart().</p>
  
  <p>What are best practices for (or existing examples of) unit tests that cover such methods?  I could, I suppose, use ob functions to capture the output, but then I'd need an external library to parse and test the HTML. Are there better ways, and are there examples already in place?</p>
  
Tags: <ajax><testing>
Title: How to write unit tests for methods thsat echo instead of return (e.g., AJAX call handlers)
ThreadText: # How to write unit tests for methods thsat echo instead of return (e.g., AJAX call handlers)
  
  (Asked by: username_0 on 2016-07-23)
  
  There are several methods in CiviCRM that just print their output directly to the buffer and then exit, because they're just sending an HTML snippet or other response for an AJAX call. For example, CRM\_Event\_Cart\_Page\_CheckoutAJAX::add\_participant\_to\_cart().
  
  What are best practices for (or existing examples of) unit tests that cover such methods? I could, I suppose, use ob functions to capture the output, but then I'd need an external library to parse and test the HTML. Are there better ways, and are there examples already in place?
  
  **Tags:** ajax, testing
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2016-07-26. Score: 2 | Accepted answer
  
  Allan - I have tended to try to refactor the AJAX functions such that they call a more testable function & test that - this generally improves the code....
  
  There are a few examples that test the calls directly - see classes like
  
  CRM\_Contact\_Page\_AjaxTest
  CRM\_Custom\_Page\_AJAXTest
  CRM\_Financial\_Page\_AjaxTest
  CRM\_Group\_Page\_AjaxTest
  
  
  ---
  
  ### Answer by username_2 on 2016-07-25. Score: 2
  
  I'm a bit out of date on the current status of the civi test framework, but if WebTests are still available I would do this as a WebTest.
  <https://wiki.civicrm.org/confluence/display/CRM/Setting+yourself+up+to+work+with+Selenium+tests>
  
  Include a line that waits for some element you are expecting to appear, then test its value using the webtest api.
  
  > **Comments:**
  > **username_2**: It looks like they do still exist. See e.g. https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/WebTest/Contact/AddTest.php, and it looks like there's a utility function you can use $this->waitForAjaxContent().  
  > **username_1**: Webtests run - but they are of limited value as they are too slow & unreliable to run as part of CI :-(  
  
  ---
  
  