Possible to mock RemoteObject?

139 views
Skip to first unread message

Ole C Langfjæran

unread,
Feb 20, 2012, 9:51:07 AM2/20/12
to Mockolate
I see it's quite easy to mock HttpService, ref the
StubbingHttpService. But are there any examples of how to mock
RemoteObject if even possible?

Brian Kotek

unread,
Feb 20, 2012, 5:02:05 PM2/20/12
to mock...@googlegroups.com
FWIW, I've done it by mocking the method called on my service/delegate class rather than trying to mock the RemoteObject itself. For example, I have a basic utility method that I can call from my tests to mock a service method and trigger the ResultEvent. Obviously this is very generic but its meant to be an easy option to cover common situations:


/**
 * Sets up the specified mock service and method name to return a fake ResultEvent when the method is later invoked during a unit test. 
 * @param mockService The mocked object, typically a mock service or delegate.
 * @param serviceMethodName The name of the method that will be invoked on the mock service.
 * @param result An optional result object that will be used in the ResultEvent.
 * @return The AsyncToken that will be used to handle the fake asynchronous call.
 */
public static function mockServiceResult( mockService : Object, serviceMethodName : String, result : Object=null ) : AsyncToken
{
var token : AsyncToken = new AsyncToken();
var resultEvent : ResultEvent = new ResultEvent( ResultEvent.RESULT, false, false, result != null ? result : {}, token );
mock( mockService ).method( serviceMethodName ).anyArgs().returns( token ).answers( new ResultAnswer( token, resultEvent ) );
return token;
}

Ole C Langfjæran

unread,
Feb 22, 2012, 2:50:05 AM2/22/12
to Mockolate
Aha! ResultAnswer was what I was looking for!
I guess by having a Service/Delegate class on top of your RemoteObject
you are getting around the problem of trying to mock a dynamic class
as RemoteObject is? Unfortunately I'm trying to write tests for legacy
code that would make me have to rewrite quite a bit of code replacing
the RemoteObject with a Service/Delegate. Or is it possible to mock
methods that doesn't exists with Mockolate?

Thanks for the nice code anyways - it was a great help!

//OC

Drew Bourne

unread,
Feb 22, 2012, 5:59:30 AM2/22/12
to mock...@googlegroups.com
I've written up an example of mocking RemoteObjects. https://gist.github.com/1883628

It's a bit tricky because dynamic Proxy subclasses need to intercept methods from the flash_proxy namespace such as callProperty() which requires telling Mockolate to proxy those namespaces, and to use nsMethod() to define the expectations. 

It could be made easier, although I kind of like that the complicated nature of it should be telling you the API of the class you are mocking is too tricky. 

If this example isn't suitable, could you include a snippet of how you setup and call the RemoteObjects in your project. 

cheers, 
Drew

Ole C Langfjæran

unread,
Feb 22, 2012, 8:38:08 AM2/22/12
to Mockolate
Brilliant! Should maybe have included this on the mockolate.org under
Decorations?
Allthough the gist works perfectly standalone when I run the gist in
my project, it does not work as nice with the legacy code.
I've got an unmet Expectation. Is it possible for me to debug what the
actual input params were? Maybe I could tweak the
mockolate.ingredients.Expectation.toString() in some way?
#not-to-familiar-within-these-hamcrestmatcher-stuff

//OC

On Feb 22, 11:59 am, Drew Bourne <d...@firstbourne.com> wrote:
> I've written up an example of mocking RemoteObjects.https://gist.github.com/1883628
>
> It's a bit tricky because dynamic Proxy subclasses need to intercept
> methods from the flash_proxy namespace such as callProperty() which
> requires telling Mockolate to proxy those namespaces, and to use nsMethod()
> to define the expectations.
>
> It could be made easier, although I kind of like that the complicated
> nature of it should be telling you the API of the class you are mocking is
> too tricky.
>
> If this example isn't suitable, could you include a snippet of how you
> setup and call the RemoteObjects in your project.
>
> cheers,
> Drew
>

Ole C Langfjæran

unread,
Feb 22, 2012, 10:13:52 AM2/22/12
to Mockolate
Found the problem. Had a typo in [Mock(namespaces="remoteObjectNSs")].
Managed to write [Mock(namespacec...

The unmet expectations just threw me off the trail.

Thx 4 saving the day!

Drew Bourne

unread,
Feb 22, 2012, 5:41:03 PM2/22/12
to mock...@googlegroups.com
Glad you've got it working. 

Thanks for giving me an idea: add some error reporting in case there are unsupported attributes in the [Mock] metadata.

I don't use RemoteObjects so not too sure of the nuances of their usage. Would a decorator much like the HTTPServiceDecorator be appropriate? And yes this example should go onto mockolate.org.

cheers, 
Drew

Ole C Langfjæran

unread,
Feb 23, 2012, 4:14:43 AM2/23/12
to Mockolate
I would believe a decorator would be most welcomed!
Maybe I could try to put some effort back and fork a little bit. I've
used https://github.com/blegros/flexRpcStubs from time to time. Maybe
some of his ideas could be useful. And do you have some heads up to
where to start to implement the an errormessage who also prints the
mismatched arguments in addition to the matching?

//OC

On Feb 22, 11:41 pm, Drew Bourne <d...@firstbourne.com> wrote:
> Glad you've got it working.
>
> Thanks for giving me an idea: add some error reporting in case there are
> unsupported attributes in the [Mock] metadata.
>
> I don't use RemoteObjects so not too sure of the nuances of their usage.
> Would a decorator much like the HTTPServiceDecorator be appropriate? And
> yes this example should go onto mockolate.org.
>
> cheers,
> Drew
>
Reply all
Reply to author
Forward
0 new messages