The short question: Is there a way to reset a Mock object in SimpleTest, removing all expectations?
The longer explanation:
I have a class that I'm testing using SimpleTest and am having some problem with the Mock objects it is using.
The class is a Logger
, and inside the logger are a number of Writer
objects (FileWriter
, EmailWriter
, etc). Calling the Logger::log()
method performs some logic behind the scenes and routes the message to the correct writer. Writers are cached in the Logger class to save re-instantiating each one each time.
In my unit tests, I set up a Logger, create and add some Mock Writer objects to it and then have been using methods like MockDBWriter->expectOnce()
to test that the Logger is working.
The problem now is that I want to test another function of the Logger, but the expectOnce
expectations are still in effect and causing my subsequent tests to fail.
function testWritesMessageOK() {
$log = Logger::getInstance();
$mock = new MockFileWriter($this);
$log->addWriter($mock);
$mock->expectOnce("write", "Message");
$log->write("Message"); // OK
}
// this is just an example - the actual test is much less inane
function testNumberOfWrites() {
$log = Logger::getInstance();
$mock = $log->getWriter();
$mock->expectCallCount('write', 2);
$log->write("One"); // fail - it doesn't match "Message"
$log->write("Two");
}
Is there a way to reset a Mock object, removing all expectations?