Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-17-2007, 01:52 PM
Junior Member
 
Join Date: Jul 2007
Location: Brisbane, Australia
Posts: 9
Default Avoiding Dependencies to Ease Allow Testing

I have been experimenting with the Zend Framework for a while now, and have just been given an opportunity to oversee a few projects (when it rains it pours). I am taking a leap and have decided to develop them all on the framework.

I would like to develop at least one (possible a couple) of these projects in a Test Driven Development (TDD) style but am having some difficulty in producing controllers that are loosely coupled enough to allow unit tests.

I am making heavy use of dependency injection to loosen the coupling between controllers and models, and controllers and views. But, the controller has a need to instantiate the objects that are passed as dependencies. As such, it makes it difficult to inject mocks to unit test the controllers.

eg.
Code:
public function loginAction()
{
...
    //Instantiate the Users Model
    $this->_users = new Auth_Users();

    //Create the Auth_Adapter
    $authAdapter = new Zend_Auth_Adapter_DbTable($this->registry->get('database'), self::USERS_TABLE, self::IDENTITY_COLUMN, self::CREDENTIAL_COLUMN);

    //Attempt Validation
$this->_users->authenticate($authAdapter, $username, $password);
...
}
As you can see, the Auth_User class is loosely coupled by injecting the Auth_Adapter from the controller, and as such can be tested using a mock Auth_Adapter. But I cannot figure out a way to unit test the controller.

Am I being too strict with my expectation of unit testing every element? Is it relatively safe to ignore unit testing for the controllers, and only perform integration testing with the models? or even forget testing controllers all-together? (I would guess not)

or is there another solution for decoupling? (I can't find it, obviously the objects have to be instantiated somewhere)

I hope someone can offer some insight into a solution, I wouldn't want this to be the reason I can't convince my team/boss to use Zend and/or TDD.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 07-24-2007, 05:23 AM
Junior Member
 
Join Date: Jul 2007
Location: Brisbane, Australia
Posts: 9
Default

Well, I have done some more searching and have come to the conclusion that I probably was trying too hard (guess thats better than the opposite).

Unit testing a controller class completely independant of any other elements is somewhat unrealistic, due to the problems I encountered due to not being able to substitute in mock objects. It seems it is acceptable to perform what I would consider integration testing on a controller instead. For models (and probably views) the independant level of testing is possible.

I still haven't found many resources on how to go about it, but there is a discussion on Nabble that covers it pretty well, as well as this article by Alexander Netkachev.
__________________
Brenton Alker
Brisbane, Australia

http://blog.tekerson.com/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 05:18 PM.