drewbourne / mockolate

fake chocolate, mock objects and test spies for AS3
http://mockolate.org/
MIT License
145 stars 27 forks source link

HttpService mocking problem - and solution #47

Open MurrayFurtado opened 13 years ago

MurrayFurtado commented 13 years ago

Drew,

I've just started using Mockolate, which seems to me to be the most fully featured Flex mocking framework out there, and I have been wrestling with getting a mock HttpService working.

Here's the consumer of the service:

public class LoadEmployeesDelegate 
{
    private var responder : IResponder;
    public var service : HTTPService;

    public function LoadEmployeesDelegate( responder : IResponder ) 
    {
        this.service = new HTTPService();
        this.service.url="assets/Employees.xml";
        this.responder = responder;
    }

    public function loadEmployeesService() : void 
    {
        var token:AsyncToken = service.send();
        token.addResponder( responder );
    }
}

The first approach I used was:

[Rule] public var rule:MockolateRule = new MockolateRule();

[Mock] public var httpService : HTTPService;

... // no send parameters so set to null // don't care about the fault details so set blank mock(httpService).asHTTPService().send(null).fault("","","");

This didn't work because it failed to return an AsyncToken :(

I then tried the following which did work :)

var token:AsyncToken = new AsyncToken(); var faultEvent:FaultEvent = new FaultEvent(FaultEvent.FAULT, false, false, new Fault("", "", ""), token); mock(httpService).method("send").returns(token).answers(new FaultAnswer(token, faultEvent));

My question is, why did the first example fail, and what is the purpose of the asHTTPService() method. Is there any way to refactor the failing example so that it returns the AsyncToken without the verbose setup of the second?

Many thanks,

Murray