views:

82

answers:

2

Hello,
I want to make an application for iPhone, as an introduction to programming in ObjC and Cocoa. I'm .net developer, so programming on Mac is a whole new world to discover for me :)

My application will be talking with web service. I want to use iCuke as a testing framework, and I don't want to connect to that webservice every time I ran test suite. So how can I mock a webservice response for testing? One solution could be use of SoapUI, but maybe there is some solution that do not use external tools.

+1  A: 

I'm not sure if my solution is appropriate for your needs, but maybe my input is of help here. I use quite a few different webservices in my app, all of them using an xml output, so what i did for testin purposes was writing mock-xml files and within my app instead of using the webservice itself using static test-xmls placed on a webserver. I did this mainly because the xml output was defined and so i could implement my parser and the rest of the app while the service itself was being developed at the same time.


cheers

sam

samsam
that's exactly the same way that I did
vodkhang
+1  A: 

I would write a wrapper around the calls to the webservice in the application.
Example in Pseudo Code

CallWebService (action, options,...) {
    // Code for connectiong to Webservice
}

Then you just mock of that function just you would like any other function

CallWebService (action, options,...) {
    return true;
}

This way you can mock of the webservice without bothering about it being a webservice or a database connection or whatever. And you can have it return true or whatever.

Good luck

UPDATE

To take this idea one step further and make your tests even more powerful you could use some kind of test parameters or environment parameters to control what happens in the mocked off webservice method. Then you can successfully test how your codes handels different responses from the web services.
Again in pseudo-code:

CallWebService (action, options,...) {
    if TEST_WEBSERVICE_PARAMETER == CORRUPT_XML
        return "<xml><</xmy>";
    else if TEST_WEBSERVICE_PARAMETER == TIME_OUT
        return wait(5000);
    else if TEST_WEBSERVICE_PARAMETER == EMPTY_XML
        return "";
    else if TEST_WEBSERVICE_PARAMETER == REALLY_LONG_XML_RESPONSE
        return generate_xml_response(1000000);
}

And tests to match:

should_raise_error_on_empty_xml_response_from_webservice() {
    TEST_WEBSERVICE_PARAMETER = EMPTY_XML;
    CallWebService(action, option, ...);
    assert_error_was_raised(EMPTY_RESPONSE_FROM_WEBSERVICE);
    assert_written_in_log(EMPTY_RESPONSE_LOG_MESSAGE);
}
...

And so on, you get the point. Please note that all my examples are Negative test cases but this could of course be used to test Positive test cases also.

Jonas Söderström
I think this is good solution. I made a @protocol, and two @interface's which implements it. Seems that it works :)
matma
Glad you liked the response. I've updated it with a way to make the tests even more powerful, to test your systems handling of the response from the Web Services.
Jonas Söderström