|
|||
|
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);
...
}
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. |
|
|||
|
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. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|