Anyone handy with UnitTest's?

Caporegime
Joined
18 Oct 2002
Posts
29,493
Location
Back in East London
I have a PHP method (from my controller class):
Code:
function execute ($route = null)
{
    do
    {
        $route = $this->getRouter()->execute($route);
    } while (!is_null($route));
}

That I need to unit test - but I'm not sure how.. The Router is a mock actor as that hasn't been implemented yet. It will return null on 'success' or a new instance of itself on failure (ergo, a new route.) The mock object simply returns a new instance (well, the same instance actually..) on the first iteration, and null on the second.
Code:
Mock::generate('Router');
$router = new MockRouter;
$router->setReturnValueAt(0, 'execute', $router);
$router->setReturnValueAt(1, 'execute', null);
$controller->setRouter($router);

I'm not sure how or where I can squeeze in an assert/expect to test?

Any suggestions welcome :)
 
Solved ..

Code:
    public function TestExecute ()
    {
        $this->_mockRoute = new MockRouter();
        $this->_mockRoute->setReturnValueAt(0, 'execute', new MockRouter);
        $this->_mockRoute->setReturnValueAt(1, 'execute', null);
        $this->_mockRoute->expectAt(0, 'execute', array(null));
        $this->_mockRoute->expectAt(1, 'execute', array(new MockRouter));
        $this->_mockRoute->expectCallCount('execute', 2);
        $this->_front->setRouter($this->_mockRoute);
        $this->_front->execute();
    }
 
Last edited:
Back
Top Bottom