Page 1 of 1

Debugging Tests...

Posted: Sun Jul 31, 2005 2:36 pm
by nielsene
So I'm trying to run a few more Mock-based tests and am running into Fatal errors, reported in the simpletest code. I'm 99% sure its because I'm not using the Mocks right, but I'm not sure how to proceed.

Here's the test:

Code: Select all

function TestOneEntry() {
    $registry=&Registry::instance();

    $db = new MockDB();
    $db->setReturnValue('query','The Phrase',array());
    $db->expectOnce('query',array('The Phrase'));
    $registry->register("Competition Database",$db);

    $pb= new MockPhraseBook();
    $pb->setReturnValue('getPhrase','The Phrase',array("Event Registrations"));
    $pb->expectOnce('getPhrase',array('Event Registrations'));
    $registry->register("Export Phrase Book",$pb);

    $mockedList = "[Mock EventEntryFormatter Output]";
    $eventEntryFormatter = new MockEventEntryListFormatter();
    $eventEntryFormatter->setReturnValue('formatAsTabbed',$mockedList);

    $expectedOutput=<<<END_OUTPUT
$this->listName
{$this->row1[1]}. {$this->row1[2]}
$mockedList

END_OUTPUT;
    $this->assertEqual($expectedOutput,
		       $this->formatter->formatAsTabbed($this->oneItemResult,
							$eventEntryFormmatter));
  }
Here's the production code, with my current debugging print_r's

Code: Select all

function formatAsTabbed($qr,$nestedFormatter) {
        $str = "LIST OF EVENTS\n";
    $numRows=$qr->numRows();
    if (0==$numRows)
      $str.="No Events\n";
    else {
      for ($i=0;$i<$numRows;$i++) {
	list($eventID,$progNum,$eventName) = $qr->getRowAt($i);
	$str.="$progNum.$eventName\n";
	$registry = Registry::instance();
	echo "<pre>";
	print_r($registry);
	$pb = $registry->getResource("Export Phrase Book");
	
	print_r($pb);
	$db = $registry->getResource("Competition Database");
	print_r($db);
	$query = $pb->getQuery("Event Registrations",
			       array("eventid"=>$eventID));
	echo "123";
	$subResult = $db->query($query);
	echo "4";
	$str.= $nestedFormatter->formatAsTabbed($subResult);
	echo "5";
	echo "</pre>";
	$str.="\n";
      }
    }
    return $str;
   
  }
I receive a

Code: Select all

Fatal error:  Call to a member function on a non-object in /usr/local/CIB/simpletest/mock_objects.php on line 914
line 20 of the production code is run, line 21 is where the error appears to be triggered. The print_r's show that the db is the configured MockDB.

Posted: Sun Jul 31, 2005 3:08 pm
by McGruff
You need to pass "$this" to mocks when they are instantiated:

Code: Select all

$foo =& new MockFoo($this);
Does that fix it?

Posted: Sun Jul 31, 2005 3:19 pm
by nielsene
McGruff wrote:You need to pass "$this" to mocks when they are instantiated:

Code: Select all

$foo =& new MockFoo($this);
Does that fix it?
I "knew" that. Thanks.... Yes, that fixes it.