Page 1 of 1

Testing File dependencies and mocking

Posted: Sat Mar 31, 2007 8:32 pm
by John Cartwright
Using simpletest,

I'm trying to test a part of my application layer over the zend framework, which essentially checks to make sure the view or model classes exist before we initialize the object. I'm using class_exists in combination with autoload to acheive this.

Code: Select all

protected function _factoryComponent($name, $type)
	{	
		$component = 'Application_'. $type .'_'. ucfirst($name);
		if (!class_exists($component)) {
			throw new Northern_Component_Exception('Cannot find "'. $component .'" component');
		}
		
		return new $component();	
	}
So $component may equal "Application_View_Index" or "Application_Model_Index", etc. It is my understanding mocks need a base class to generate the mock. Considering the views and models are found in the application, and arn't even required to exist, how do I mock a class that doesn't exist? Do I just create a bare class Application_View_Index and mock it just for the sake of testing?

Posted: Sat Mar 31, 2007 9:44 pm
by Chris Corbyn
Do all these classes have a common interface? If so, force them to be subclasses of some abstract and mock the abstract class. If they don't have a common interface then what exactly are you testing? :)

Posted: Sat Mar 31, 2007 10:06 pm
by John Cartwright
No they do not have a common interface. In the grand scheme of things, the method I posted is essentially a factory to a composite classes (the model and the view) because I wanted to support being able to load multiple views or models into a controller.

So, what I'm trying to test is essentially the behavior of if a class isn't there, or if a component was returned.. more so to make if things are refactored, the class doesn't break.

I'm not actually testing this method I've posted, obviously, it's just that the class has a dependency on certain classes (models, views, etc) being loaded (which I'm trying to mock). Now, it is my understanding that testing should be completely indepedent from it's environment.. so if I want to test that a component is returned from the factory, how would I if the class doesn't exist.

My test would look something like

Code: Select all

public function testAttachingComponent() 
	{
		$component = new Northern_Component();
		
		try {
			$component->attachComponent('Index', 'View');
		} catch (Exception $e) {
			$this->fail();
		}	
	}
So the $component object would autoload Application_View_Index, although it won't exist in the test suite. Would I have to simply include

Code: Select all

class Application_View_Index { }
at the top of this particular test case?

To complicate things furthur, I'm looking to check if the component classes (views, etc) have certain properties, and if they do.. test something else. So lets say I figure out how to mock this properly, is it possible to inject properties into the class? I know it's possible to inject return values of methods.. hrm..

Hope that even made sense :wink:

Thanks.