views:

67

answers:

1

Hello people,

I am diving into the world of unit testing. And i am sort of lost. I learned today that unit testing is testing if a function works.

I wanted to test the following function:

public function getEventById($id)
{
    return $this->getResource('Event')->getEventById($id);
}

So i wanted to test this function as follows:

public function test_Event_Get_Event_By_Id_Returns_Event_Item()
{
    $p = $this->_model->getEventById(42);
    $this->assertEquals(42, EventManager_Resource_Event_Item_Interface);
    $this->assertType('EventManager_Resource_Event_Item_Interface', $p);
}

But then I got the error:

1) EventTest::test_Event_Get_Event_By_Id_Returns_Event_Item
Zend_Db_Table_Exception: No adapter found for EventManager_Resource_Event

/home/user/Public/ZendFramework-1.10.1/library/SF/Model/Abstract.php:101
/var/www/nrka2/application/modules/eventManager/models/Event.php:25

But then someone told me that i am currently unit testing and not doing an integration test. So i figured that i have to test the function getEventById on a different way. But I don't understand how. What this function does it just cals a resource and returns the event by id.

+2  A: 

Well, one way is by overloading the object you're testing. Then you can override the getResource() method to return a "Mock" object (one that always behaves deterministically, and that you can "set up" for each test). The trick is to isolate the functionality that you want to test, and remove any dependencies that may get in the way (the getResource method in your case)...

I'd recommend reading some tutorials (not just one, a few), and trying to understand the philosophy as well as the methodology...

Some Tutorials:

Zend Developer Zone

PHP Unit Slideshow

Pragmatic Unit Testing (Note, this is for C#, but the concepts should be the same)

DevShed

ircmaxell
Thanks for all the information. I need to so still a lot of reading!
sanders