Reputation: 513
All,
I'm using Cucumber for acceptance testing a Ruby command line utility. This utility pulls data in from a webservice.
I understand Cucumber is for acceptance testing and tests the whole stack but I still need to provide consistant replies from the webservice.
Should I mock the webservice? If yes, how? What's the best approach here?
Cheers, Gordon
Upvotes: 3
Views: 1776
Reputation: 4916
Mock of the Webservice
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.
Test how your code handles responses from the Webservice
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 even though all my examples are Negative test cases this could of course be used to test Positive test cases also.
Do note that this is a copy of an answer i made to a similar questions: Mockup webservice for iPhone
Good luck
Upvotes: 0
Reputation: 513
So after a bit of thinking! Then a bit of googling I found FakeWeb. Does exactly what I needed!
Check out Dr Nic's slides - especially slide 17.
And it was easy - in under 2 hours I've managed to set it up, rewrite my tests, get everything passing and check it all back in to git hub!!
HTH others!
Upvotes: 2
Reputation: 20934
I am not very familiar with Ruby or Cucumber, so I can only give you a very general answer related to your problem, and it actually has more questions than answers.
Those are just a few points, but at least the last three I always consider before choosing to mock or not to mock.
Upvotes: 1