View Single Post
  #1 (permalink)  
Old 07-17-2007, 01:52 PM
Deprecated Deprecated is offline
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.
Reply With Quote