drewbourne / mockolate

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

HTTPServiceDecorator not verifying send parameters #24

Closed jpparkin closed 14 years ago

jpparkin commented 14 years ago

Hi,

I've written the following unit test following the example in StubbingHttpService.as file. When I run it it passes which it shouldn't because the HTTPService.send() method isn't being called within the actual code. My expectation is for an ExpectionError to be thrown.

What am I doing wrong?

[RunWith("mockolate.runner.MockolateRunner")] public class DVBIPBridgeServiceTest {

[Mock]
public var httpService : HTTPService;

[Test(async)]
public function testExecuteRequestReturnsJSONData() : void {
    var expectedData : String = "[\"OK\", \"TIME\", {\"localtime\": [2010, 9, 21, 11, 22, 23, 1, 264, 1], \"asctime\": \"Tue Sep 21 11:22:23 2010\", \"time\": 1285064543.01}]";

    mock(httpService).asHTTPService().method("GET").send(hasProperties({command : "time"})).result(expectedData);

    var service : DVBIPBridgeService = new DVBIPBridgeService(httpService);

    Async.handleEvent(this, service, DVBIPBridgeService.REQUEST_COMPLETE, function(event:Event,data:Object) : void {
        var requestData : Object = service.getData();
        assertThat(requestData[1], equalTo("TIME"));
        assertThat(requestData.localtime.time, equalTo(1285064543.01));
    });

    service.makeRequest({command : "time"});

}

} }

drewbourne commented 14 years ago

If the HTTPService.send() isn't being called then the DVBIPBridgeService.REQUEST_COMPLETE event shouldn't be dispatched which means the test should fail with a 'Timeout Occurred before expected event' error.

Can you include the DVBIPBridgeService.makeRequest() method or the whole class? More context will help.

jpparkin commented 14 years ago

Thank you for your response drewbourne. I can see what you are saying and makeRequest() is dispatching the REQUEST_COMPLETE (without it I do get a Timeout Occurred error) event but this is the only thing it is doing. I'm trying to drive my implementation through the test writing the simplest code possible to make the test pass.

public function makeRequest(params : Object) : void { dispatchEvent(new Event(REQUEST_COMPLETE)); }

getData() is returning some hard coded values at present but that will change with the introduction of more tests. The most important check I'm trying to make is that the HTTPService is being called with the correct parameters.

Thanks.

jpparkin commented 14 years ago

Here is the class as it currently stands:

public class DVBIPBridgeService extends EventDispatcher{ static const REQUEST_COMPLETE:String = "onRequestComplete"; private var service:HTTPService; public function DVBIPBridgeService(service : HTTPService) { this.service = service; }

function getData():Object {
    var data:Array = [];
    data[1] = "TIME";
    data.localtime = new Object();
    data.localtime.time = 1285064543.01;
    return data;
}

function makeRequest(requestParams:Object):void {
    dispatchEvent(new Event(REQUEST_COMPLETE));
}

} }

drewbourne commented 14 years ago

Found the cause, mock(target).asHTTPService() was forcing the Expectations to be stubs not mocks and thus not fail when verified. I've pushed changes to master.

Thanks.