Validating HTML Fragments with SimpleTest

Events happening in the community are now at Drupal community events on www.drupal.org.
Posted by starbow on March 29, 2007 at 7:11pm

This is the best bit from my blog post: http://starbowconsulting.com/blog/tao/simpletest-tips
(Also, if anyone want to read me rant about the state of unit testing in drupal: http://starbowconsulting.com/blog/tao/joys-unit-testing)
Validating HTML Fragments
I am currently working on code that generates html fragments. Simple Test out-of-the-box does a great job helping you test entire http pages, but only it if goes and gets them itself. I could not find any easy way to load in a local page/fragment. So I did it the hard way:

<?php
function setUp() {
require_once(
SIMPLE_TEST . '/mock_objects.php');
Mock::generate( 'SimpleHttpResponse' );
}

function
setSource( $html ) {
$this->_mockResponse = new MockSimpleHttpResponse();
$this->_mockResponse->setReturnValue( 'isError', false );
$this->_mockResponse->setReturnValue( 'getContent', $html );
$this->_browser->_page = $this->_browser->_parse( $this->_mockResponse );
}
?>

Now, in my tests, I can say great things like this:

<?php
$html
= drupal_get_form( $form_id );
$this->assertNull( form_get_errors() );
$this->setSource( $html );
$this->assertText( 'bar' );
$this->assertField( 'setme', 'one');
$this->assertNull( $this->_browser->getField('added') );
?>
Categories: ,

Testing and Quality Assurance

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

AltStyle によって変換されたページ (->オリジナル) /