PHPUnit question concerning mock objects...
Posted: Tue Nov 27, 2007 10:52 am
I've found that the PHPUnit documentation doesn't cover as much as it probably should, and it leaves a few things out entirely, making assumptions that you know what it's talking about.
One such instance is when it's discussing mock objects (I know, PHPUnit's mock object support isn't as good as SimpleTest's, but my company switched from SimpleTest to PHPUnit for some reason, so I need to learn to cope)...I guess my main question is where and how do I get access to the $subject->attach() method that it examples? This is from here: http://www.phpunit.de/pocket_guide/3.2/ ... jects.html, and the excerpt:
Most of this I understand just fine, but where is this
coming from? The way I see it, $subject is the object that i'm testing, while $observer is using PHPUnit's mock API to observe the subject. But where does ->attach() come from? It certainly isn't a function that is on my actual object under test, and trying to actually say new Subject says that such an object doesn't exist (so it's not something PHPUnit made up).
Does anyone know how I can attach the two? Or rather, what $subject is, and where the attach() method comes from?
Also, I really hope you guys get that PHPMock framework up and running, it looks awesome and I will definately use it over this weird PHPUnit mock implementation.
One such instance is when it's discussing mock objects (I know, PHPUnit's mock object support isn't as good as SimpleTest's, but my company switched from SimpleTest to PHPUnit for some reason, so I need to learn to cope)...I guess my main question is where and how do I get access to the $subject->attach() method that it examples? This is from here: http://www.phpunit.de/pocket_guide/3.2/ ... jects.html, and the excerpt:
Code: Select all
<?php
require_once 'PHPUnit/Framework.php';
class ObserverTest extends PHPUnit_Framework_TestCase
{
public function testUpdateIsCalledOnce()
{
// Create a Mock Object for the Observer class
// mocking only the update() method.
$observer = $this->getMock('Observer', array('update'));
// Set up the expectation for the update() method
// to be called only once and with the string 'something'
// as its parameter.
$observer->expects($this->once())
->method('update')
->with($this->equalTo('something'));
// Create a Subject object and attach the mocked
// Observer object to it.
$subject = new Subject;
$subject->attach($observer);
// Call the doSomething() method on the $subject object
// which we expect to call the mocked Observer object's
// update() method with the string 'something'.
$subject->doSomething();
}
}
?>
Code: Select all
$subject = new Subject;
$subject->attach($observer);
Does anyone know how I can attach the two? Or rather, what $subject is, and where the attach() method comes from?
Also, I really hope you guys get that PHPMock framework up and running, it looks awesome and I will definately use it over this weird PHPUnit mock implementation.