I'm trying to mock FileReference in a flex project I'm working on.
Stubbing out some properties, such as name and size works fine, but data just refuses to work. Here is code that illustrates the problem:
[Test]
public function FileReferenceMockTest():void {
var frMock:FileReference = nice(FileReference);
var bytes:ByteArray = new ByteArray();
stub(frMock).getter("data").returns(bytes);
stub(frMock).getter("name").returns("filename");
assertThat(frMock.name, equalTo("filename")); // works
assertThat(frMock.data, sameInstance(bytes)); // Fails and says that frMock.data is null
}
If I set a breakpoint and check frMock in the debugger it looks like this:
frMock
[inherited]
data = null
creationDate = null
creator = null
modificationDate = null
name = "filename"
__proxy__ = InterceptorProxyListener
...
As you can see, data is treated differently by the mock.
I've been banging my head against this problem for several hours now and could really really use some help figuring this out.
Update:
The code works when run either as a Flex Library Project or a Flex Project (Air). It's when the code is run in a browser plugin that it fails.
A check in the debugger in the library project also shows that the data property is not nested inside [Inherited], but on the same level as the other properties.
Update:
I encountered the same problem with URLLoader, but this time the other way around. Mocking of the URLLoader only works when running the tests in a browser plugin.
Since I'm creating a class to handle local and remote file loading, using either FileReference or URLLoader, depending on input, I'm effectively prevented from unit testing this class completely. :(