Jump to content

Unit test an awkward-to-be-tested resources


ang89

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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);
  }

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.