OK I've finished up my initial work on ColdMock and have released it as a project on RIAForge. The zip file and SVN repository contain the ColdMock factory as well as some examples of how it can be used.
I added what I think is a crucial element: validation of arguments and return values for the mocked methods. This was actually pretty challenging to add. This is because once the MockFactory creates an instance of your component, it purges all of the existing methods from that instance. It must do this so that your original methods don't run, since that defeats the whole point of using a Mock object. You don't want the original methods to run, since they will have code in them or references to other objects in them. Mocks just return the dummy data you specify. However, purging all the existing methods also means all the arguments, argument types, and return types are also gone.
To work around this, the Mock object uses the original component's metadata. When you call a method, onMissingMethod() runs. It then loops through the metadata and checks that all required incoming arguments are present, that the types are correct (if you specified a type in the original argument), and that the return value that you have specified is of the correct type as well (if you specified an original return type).
The possible combinations of untyped arguments, optional arguments, named vs. positional arguments, native types vs. CFC types, etc. made this quite difficult to implement. Basically, I had to write code that mimics all of CF's built in required argument and type checking! However, I believe I have it working for all of those situations. If anyone runs into a situation that isn't handled properly, please submit an issue at the RIAForge project.
del.icio.us
|
Digg It!
|
Linking Blogs
| 3665 Views

