The actionscript I want to write looks like this:
public function API(requestClass:Type=URLLoader) {
var req:URLLoader = new requestClass(new URLRequest("some url"));
req.load(url);
//etc
}
so that I can test the API class by passing in a mocked subclass of URLLoader. This doesn't appear to be possible in Actionscript's type system.
Alternatively, it could be sufficient to change the URLLoader's load() method at runtime. I had high hopes for this code in a test method:
var b:Array = [];
URLLoader.prototype.load = function(u:URLRequest):void {
b.push(u);
}
(new URLLoader()).load(new URLRequest("http://localhost"));
assertEquals(b.length, 1);
but URLLoader does in fact call the url it's given, and b.length == 0.
So! Is there any way that I can write my API class to be testable without putting the testing logic within my API class? Have I missed something obvious?