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

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?

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

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.