ang89 Posted June 9, 2010 Share Posted June 9, 2010 Hi there, I want to know what is the best practice to unit test an awkward-to-be-tested resources, like a web service (e.g. Facebook API). I've been reading a lot about "stub object", which is described as an object that simulates the behaviour of an existing object. So instead of actually post something to a user's Facebook wall, it does nothing. However, when this stub object has to return large amount of data (e.g. user's friend list and their birthday), how do you provide the data? Hard-code the list into the stub object class definition? Create a fixture file (maybe in XML, or CSV) to store the data? Best regards, Andree Quote Link to comment https://forums.phpfreaks.com/topic/204263-unit-test-an-awkward-to-be-tested-resources/ Share on other sites More sharing options...
Mchl Posted June 9, 2010 Share Posted June 9, 2010 Create a fixture file (maybe in XML, or CSV) to store the data? Quote Link to comment https://forums.phpfreaks.com/topic/204263-unit-test-an-awkward-to-be-tested-resources/#findComment-1069820 Share on other sites More sharing options...
ang89 Posted June 9, 2010 Author Share Posted June 9, 2010 Create a fixture file (maybe in XML, or CSV) to store the data? Hi Mchl, thanks for the answer! Have you ever use this method (stub object + fixture file) before, by any chance? Do you also use this to test database resources? Sorry for asking these questions, but I need to know what is commonly done in practice? Quote Link to comment https://forums.phpfreaks.com/topic/204263-unit-test-an-awkward-to-be-tested-resources/#findComment-1069845 Share on other sites More sharing options...
Mchl Posted June 9, 2010 Share Posted June 9, 2010 What I do is create a 'Model' class that takes 'DataProvider' object to request data from database or remote source. For tests, I can supply a mock 'DataProvider' that simply reads data from flat file. This works pretty well for testing my 'Model' classes. Actual 'DataProvider' classes have to be tested separately of course. Something like this. <?php interface DataProvider { public function load(Model $model); public function save(Model $model); //etc... } class Model { private $dataProvider; public function setDataProvider(DataProvider $dp) { $this->dataProvider = $dp; } public function loadData() { $this->dp->load($this); } } Quote Link to comment https://forums.phpfreaks.com/topic/204263-unit-test-an-awkward-to-be-tested-resources/#findComment-1069849 Share on other sites More sharing options...
ang89 Posted June 9, 2010 Author Share Posted June 9, 2010 Sounds good. Thanks! I'll give a try on PHPUnit and its mocking library. Quote Link to comment https://forums.phpfreaks.com/topic/204263-unit-test-an-awkward-to-be-tested-resources/#findComment-1069873 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.